

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>What’s New in Python 2.6 &mdash; Python v2.7.8 documentation</title>
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.7.8',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python v2.7.8 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python v2.7.8 documentation" href="../index.html" />
    <link rel="up" title="What’s New in Python" href="index.html" />
    <link rel="next" title="What’s New in Python 2.5" href="2.5.html" />
    <link rel="prev" title="What’s New in Python 2.7" href="2.7.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
 
    

  </head>
  <body>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="2.5.html" title="What’s New in Python 2.5"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="2.7.html" title="What’s New in Python 2.7"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python v2.7.8 documentation</a> &raquo;
        </li>

          <li><a href="index.html" accesskey="U">What&#8217;s New in Python</a> &raquo;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="what-s-new-in-python-2-6">
<h1>What&#8217;s New in Python 2.6<a class="headerlink" href="#what-s-new-in-python-2-6" title="Permalink to this headline">¶</a></h1>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field"><th class="field-name">Author:</th><td class="field-body">A.M. Kuchling (amk at amk.ca)</td>
</tr>
</tbody>
</table>
<p>This article explains the new features in Python 2.6, released on October 1
2008.  The release schedule is described in <span class="target" id="index-0"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0361"><strong>PEP 361</strong></a>.</p>
<p>The major theme of Python 2.6 is preparing the migration path to
Python 3.0, a major redesign of the language.  Whenever possible,
Python 2.6 incorporates new features and syntax from 3.0 while
remaining compatible with existing code by not removing older features
or syntax.  When it&#8217;s not possible to do that, Python 2.6 tries to do
what it can, adding compatibility functions in a
<a class="reference internal" href="../library/future_builtins.html#module-future_builtins" title="future_builtins"><tt class="xref py py-mod docutils literal"><span class="pre">future_builtins</span></tt></a> module and a <a class="reference internal" href="../using/cmdline.html#cmdoption-3"><em class="xref std std-option">-3</em></a> switch to warn about
usages that will become unsupported in 3.0.</p>
<p>Some significant new packages have been added to the standard library,
such as the <a class="reference internal" href="../library/multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based &quot;threading&quot; interface."><tt class="xref py py-mod docutils literal"><span class="pre">multiprocessing</span></tt></a> and <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><tt class="xref py py-mod docutils literal"><span class="pre">json</span></tt></a> modules, but
there aren&#8217;t many new features that aren&#8217;t related to Python 3.0 in
some way.</p>
<p>Python 2.6 also sees a number of improvements and bugfixes throughout
the source.  A search through the change logs finds there were 259
patches applied and 612 bugs fixed between Python 2.5 and 2.6.  Both
figures are likely to be underestimates.</p>
<p>This article doesn&#8217;t attempt to provide a complete specification of
the new features, but instead provides a convenient overview.  For
full details, you should refer to the documentation for Python 2.6. If
you want to understand the rationale for the design and
implementation, refer to the PEP for a particular new feature.
Whenever possible, &#8220;What&#8217;s New in Python&#8221; links to the bug/patch item
for each change.</p>
<div class="section" id="python-3-0">
<h2>Python 3.0<a class="headerlink" href="#python-3-0" title="Permalink to this headline">¶</a></h2>
<p>The development cycle for Python versions 2.6 and 3.0 was
synchronized, with the alpha and beta releases for both versions being
made on the same days.  The development of 3.0 has influenced many
features in 2.6.</p>
<p>Python 3.0 is a far-ranging redesign of Python that breaks
compatibility with the 2.x series.  This means that existing Python
code will need some conversion in order to run on
Python 3.0.  However, not all the changes in 3.0 necessarily break
compatibility.  In cases where new features won&#8217;t cause existing code
to break, they&#8217;ve been backported to 2.6 and are described in this
document in the appropriate place.  Some of the 3.0-derived features
are:</p>
<ul class="simple">
<li>A <a class="reference internal" href="../reference/datamodel.html#object.__complex__" title="object.__complex__"><tt class="xref py py-meth docutils literal"><span class="pre">__complex__()</span></tt></a> method for converting objects to a complex number.</li>
<li>Alternate syntax for catching exceptions: <tt class="docutils literal"><span class="pre">except</span> <span class="pre">TypeError</span> <span class="pre">as</span> <span class="pre">exc</span></tt>.</li>
<li>The addition of <a class="reference internal" href="../library/functools.html#functools.reduce" title="functools.reduce"><tt class="xref py py-func docutils literal"><span class="pre">functools.reduce()</span></tt></a> as a synonym for the built-in
<a class="reference internal" href="../library/functions.html#reduce" title="reduce"><tt class="xref py py-func docutils literal"><span class="pre">reduce()</span></tt></a> function.</li>
</ul>
<p>Python 3.0 adds several new built-in functions and changes the
semantics of some existing builtins.  Functions that are new in 3.0
such as <a class="reference internal" href="../library/functions.html#bin" title="bin"><tt class="xref py py-func docutils literal"><span class="pre">bin()</span></tt></a> have simply been added to Python 2.6, but existing
builtins haven&#8217;t been changed; instead, the <a class="reference internal" href="../library/future_builtins.html#module-future_builtins" title="future_builtins"><tt class="xref py py-mod docutils literal"><span class="pre">future_builtins</span></tt></a>
module has versions with the new 3.0 semantics.  Code written to be
compatible with 3.0 can do <tt class="docutils literal"><span class="pre">from</span> <span class="pre">future_builtins</span> <span class="pre">import</span> <span class="pre">hex,</span> <span class="pre">map</span></tt> as
necessary.</p>
<p>A new command-line switch, <a class="reference internal" href="../using/cmdline.html#cmdoption-3"><em class="xref std std-option">-3</em></a>, enables warnings
about features that will be removed in Python 3.0.  You can run code
with this switch to see how much work will be necessary to port
code to 3.0.  The value of this switch is available
to Python code as the boolean variable <a class="reference internal" href="../library/sys.html#sys.py3kwarning" title="sys.py3kwarning"><tt class="xref py py-data docutils literal"><span class="pre">sys.py3kwarning</span></tt></a>,
and to C extension code as <tt class="xref c c-data docutils literal"><span class="pre">Py_Py3kWarningFlag</span></tt>.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">The 3xxx series of PEPs, which contains proposals for Python 3.0.
<span class="target" id="index-1"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3000"><strong>PEP 3000</strong></a> describes the development process for Python 3.0.
Start with <span class="target" id="index-2"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3100"><strong>PEP 3100</strong></a> that describes the general goals for Python
3.0, and then explore the higher-numbered PEPS that propose
specific features.</p>
</div>
</div>
<div class="section" id="changes-to-the-development-process">
<h2>Changes to the Development Process<a class="headerlink" href="#changes-to-the-development-process" title="Permalink to this headline">¶</a></h2>
<p>While 2.6 was being developed, the Python development process
underwent two significant changes: we switched from SourceForge&#8217;s
issue tracker to a customized Roundup installation, and the
documentation was converted from LaTeX to reStructuredText.</p>
<div class="section" id="new-issue-tracker-roundup">
<h3>New Issue Tracker: Roundup<a class="headerlink" href="#new-issue-tracker-roundup" title="Permalink to this headline">¶</a></h3>
<p>For a long time, the Python developers had been growing increasingly
annoyed by SourceForge&#8217;s bug tracker.  SourceForge&#8217;s hosted solution
doesn&#8217;t permit much customization; for example, it wasn&#8217;t possible to
customize the life cycle of issues.</p>
<p>The infrastructure committee of the Python Software Foundation
therefore posted a call for issue trackers, asking volunteers to set
up different products and import some of the bugs and patches from
SourceForge.  Four different trackers were examined: <a class="reference external" href="http://www.atlassian.com/software/jira/">Jira</a>,
<a class="reference external" href="http://www.launchpad.net">Launchpad</a>,
<a class="reference external" href="http://roundup.sourceforge.net/">Roundup</a>, and
<a class="reference external" href="http://trac.edgewall.org/">Trac</a>.
The committee eventually settled on Jira
and Roundup as the two candidates.  Jira is a commercial product that
offers no-cost hosted instances to free-software projects; Roundup
is an open-source project that requires volunteers
to administer it and a server to host it.</p>
<p>After posting a call for volunteers, a new Roundup installation was
set up at <a class="reference external" href="http://bugs.python.org">http://bugs.python.org</a>.  One installation of Roundup can
host multiple trackers, and this server now also hosts issue trackers
for Jython and for the Python web site.  It will surely find
other uses in the future.  Where possible,
this edition of &#8220;What&#8217;s New in Python&#8221; links to the bug/patch
item for each change.</p>
<p>Hosting of the Python bug tracker is kindly provided by
<a class="reference external" href="http://www.upfrontsystems.co.za/">Upfront Systems</a>
of Stellenbosch, South Africa.  Martin von Loewis put a
lot of effort into importing existing bugs and patches from
SourceForge; his scripts for this import operation are at
<a class="reference external" href="http://svn.python.org/view/tracker/importer/">http://svn.python.org/view/tracker/importer/</a> and may be useful to
other projects wishing to move from SourceForge to Roundup.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://bugs.python.org">http://bugs.python.org</a></dt>
<dd>The Python bug tracker.</dd>
<dt><a class="reference external" href="http://bugs.jython.org">http://bugs.jython.org</a>:</dt>
<dd>The Jython bug tracker.</dd>
<dt><a class="reference external" href="http://roundup.sourceforge.net/">http://roundup.sourceforge.net/</a></dt>
<dd>Roundup downloads and documentation.</dd>
<dt><a class="reference external" href="http://svn.python.org/view/tracker/importer/">http://svn.python.org/view/tracker/importer/</a></dt>
<dd>Martin von Loewis&#8217;s conversion scripts.</dd>
</dl>
</div>
</div>
<div class="section" id="new-documentation-format-restructuredtext-using-sphinx">
<h3>New Documentation Format: reStructuredText Using Sphinx<a class="headerlink" href="#new-documentation-format-restructuredtext-using-sphinx" title="Permalink to this headline">¶</a></h3>
<p>The Python documentation was written using LaTeX since the project
started around 1989.  In the 1980s and early 1990s, most documentation
was printed out for later study, not viewed online. LaTeX was widely
used because it provided attractive printed output while remaining
straightforward to write once the basic rules of the markup were
learned.</p>
<p>Today LaTeX is still used for writing publications destined for
printing, but the landscape for programming tools has shifted.  We no
longer print out reams of documentation; instead, we browse through it
online and HTML has become the most important format to support.
Unfortunately, converting LaTeX to HTML is fairly complicated and Fred
L. Drake Jr., the long-time Python documentation editor, spent a lot
of time maintaining the conversion process.  Occasionally people would
suggest converting the documentation into SGML and later XML, but
performing a good conversion is a major task and no one ever committed
the time required to finish the job.</p>
<p>During the 2.6 development cycle, Georg Brandl put a lot of effort
into building a new toolchain for processing the documentation.  The
resulting package is called Sphinx, and is available from
<a class="reference external" href="http://sphinx.pocoo.org/">http://sphinx.pocoo.org/</a>.</p>
<p>Sphinx concentrates on HTML output, producing attractively styled and
modern HTML; printed output is still supported through conversion to
LaTeX.  The input format is reStructuredText, a markup syntax
supporting custom extensions and directives that is commonly used in
the Python community.</p>
<p>Sphinx is a standalone package that can be used for writing, and
almost two dozen other projects
(<a class="reference external" href="http://sphinx.pocoo.org/examples.html">listed on the Sphinx web site</a>)
have adopted Sphinx as their documentation tool.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference external" href="http://docs.python.org/devguide/documenting.html">Documenting Python</a></dt>
<dd>Describes how to write for Python&#8217;s documentation.</dd>
<dt><a class="reference external" href="http://sphinx.pocoo.org/">Sphinx</a></dt>
<dd>Documentation and code for the Sphinx toolchain.</dd>
<dt><a class="reference external" href="http://docutils.sf.net">Docutils</a></dt>
<dd>The underlying reStructuredText parser and toolset.</dd>
</dl>
</div>
</div>
</div>
<div class="section" id="pep-343-the-with-statement">
<h2>PEP 343: The &#8216;with&#8217; statement<a class="headerlink" href="#pep-343-the-with-statement" title="Permalink to this headline">¶</a></h2>
<p>The previous version, Python 2.5, added the &#8216;<a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a>&#8216;
statement as an optional feature, to be enabled by a <tt class="docutils literal"><span class="pre">from</span> <span class="pre">__future__</span>
<span class="pre">import</span> <span class="pre">with_statement</span></tt> directive.  In 2.6 the statement no longer needs to
be specially enabled; this means that <a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a> is now always a
keyword.  The rest of this section is a copy of the corresponding
section from the &#8220;What&#8217;s New in Python 2.5&#8221; document; if you&#8217;re
familiar with the &#8216;<a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a>&#8216; statement
from Python 2.5, you can skip this section.</p>
<p>The &#8216;<a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a>&#8216; statement clarifies code that previously would use
<tt class="docutils literal"><span class="pre">try...finally</span></tt> blocks to ensure that clean-up code is executed.  In this
section, I&#8217;ll discuss the statement as it will commonly be used.  In the next
section, I&#8217;ll examine the implementation details and show how to write objects
for use with this statement.</p>
<p>The &#8216;<a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a>&#8216; statement is a control-flow structure whose basic
structure is:</p>
<div class="highlight-python"><pre>with expression [as variable]:
    with-block</pre>
</div>
<p>The expression is evaluated, and it should result in an object that supports the
context management protocol (that is, has <a class="reference internal" href="../reference/datamodel.html#object.__enter__" title="object.__enter__"><tt class="xref py py-meth docutils literal"><span class="pre">__enter__()</span></tt></a> and <a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><tt class="xref py py-meth docutils literal"><span class="pre">__exit__()</span></tt></a>
methods).</p>
<p>The object&#8217;s <a class="reference internal" href="../reference/datamodel.html#object.__enter__" title="object.__enter__"><tt class="xref py py-meth docutils literal"><span class="pre">__enter__()</span></tt></a> is called before <em>with-block</em> is executed and
therefore can run set-up code. It also may return a value that is bound to the
name <em>variable</em>, if given.  (Note carefully that <em>variable</em> is <em>not</em> assigned
the result of <em>expression</em>.)</p>
<p>After execution of the <em>with-block</em> is finished, the object&#8217;s <a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><tt class="xref py py-meth docutils literal"><span class="pre">__exit__()</span></tt></a>
method is called, even if the block raised an exception, and can therefore run
clean-up code.</p>
<p>Some standard Python objects now support the context management protocol and can
be used with the &#8216;<a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a>&#8216; statement. File objects are one example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s">&#39;/etc/passwd&#39;</span><span class="p">,</span> <span class="s">&#39;r&#39;</span><span class="p">)</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
        <span class="k">print</span> <span class="n">line</span>
        <span class="o">...</span> <span class="n">more</span> <span class="n">processing</span> <span class="n">code</span> <span class="o">...</span>
</pre></div>
</div>
<p>After this statement has executed, the file object in <em>f</em> will have been
automatically closed, even if the <a class="reference internal" href="../reference/compound_stmts.html#for"><tt class="xref std std-keyword docutils literal"><span class="pre">for</span></tt></a> loop raised an exception part-
way through the block.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">In this case, <em>f</em> is the same object created by <a class="reference internal" href="../library/functions.html#open" title="open"><tt class="xref py py-func docutils literal"><span class="pre">open()</span></tt></a>, because
<tt class="xref py py-meth docutils literal"><span class="pre">file.__enter__()</span></tt> returns <em>self</em>.</p>
</div>
<p>The <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Higher-level threading interface."><tt class="xref py py-mod docutils literal"><span class="pre">threading</span></tt></a> module&#8217;s locks and condition variables  also support the
&#8216;<a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a>&#8216; statement:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">lock</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Lock</span><span class="p">()</span>
<span class="k">with</span> <span class="n">lock</span><span class="p">:</span>
    <span class="c"># Critical section of code</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>The lock is acquired before the block is executed and always released once  the
block is complete.</p>
<p>The <tt class="xref py py-func docutils literal"><span class="pre">localcontext()</span></tt> function in the <a class="reference internal" href="../library/decimal.html#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic  Specification."><tt class="xref py py-mod docutils literal"><span class="pre">decimal</span></tt></a> module makes it easy
to save and restore the current decimal context, which encapsulates the desired
precision and rounding characteristics for computations:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">decimal</span> <span class="kn">import</span> <span class="n">Decimal</span><span class="p">,</span> <span class="n">Context</span><span class="p">,</span> <span class="n">localcontext</span>

<span class="c"># Displays with default precision of 28 digits</span>
<span class="n">v</span> <span class="o">=</span> <span class="n">Decimal</span><span class="p">(</span><span class="s">&#39;578&#39;</span><span class="p">)</span>
<span class="k">print</span> <span class="n">v</span><span class="o">.</span><span class="n">sqrt</span><span class="p">()</span>

<span class="k">with</span> <span class="n">localcontext</span><span class="p">(</span><span class="n">Context</span><span class="p">(</span><span class="n">prec</span><span class="o">=</span><span class="mi">16</span><span class="p">)):</span>
    <span class="c"># All code in this block uses a precision of 16 digits.</span>
    <span class="c"># The original context is restored on exiting the block.</span>
    <span class="k">print</span> <span class="n">v</span><span class="o">.</span><span class="n">sqrt</span><span class="p">()</span>
</pre></div>
</div>
<div class="section" id="writing-context-managers">
<span id="new-26-context-managers"></span><h3>Writing Context Managers<a class="headerlink" href="#writing-context-managers" title="Permalink to this headline">¶</a></h3>
<p>Under the hood, the &#8216;<a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a>&#8216; statement is fairly complicated. Most
people will only use &#8216;<a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a>&#8216; in company with existing objects and
don&#8217;t need to know these details, so you can skip the rest of this section if
you like.  Authors of new objects will need to understand the details of the
underlying implementation and should keep reading.</p>
<p>A high-level explanation of the context management protocol is:</p>
<ul class="simple">
<li>The expression is evaluated and should result in an object called a &#8220;context
manager&#8221;.  The context manager must have <a class="reference internal" href="../reference/datamodel.html#object.__enter__" title="object.__enter__"><tt class="xref py py-meth docutils literal"><span class="pre">__enter__()</span></tt></a> and <a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><tt class="xref py py-meth docutils literal"><span class="pre">__exit__()</span></tt></a>
methods.</li>
<li>The context manager&#8217;s <a class="reference internal" href="../reference/datamodel.html#object.__enter__" title="object.__enter__"><tt class="xref py py-meth docutils literal"><span class="pre">__enter__()</span></tt></a> method is called.  The value returned
is assigned to <em>VAR</em>.  If no <tt class="docutils literal"><span class="pre">as</span> <span class="pre">VAR</span></tt> clause is present, the value is simply
discarded.</li>
<li>The code in <em>BLOCK</em> is executed.</li>
<li>If <em>BLOCK</em> raises an exception, the context manager&#8217;s <a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><tt class="xref py py-meth docutils literal"><span class="pre">__exit__()</span></tt></a> method
is called with three arguments, the exception details (<tt class="docutils literal"><span class="pre">type,</span> <span class="pre">value,</span> <span class="pre">traceback</span></tt>,
the same values returned by <a class="reference internal" href="../library/sys.html#sys.exc_info" title="sys.exc_info"><tt class="xref py py-func docutils literal"><span class="pre">sys.exc_info()</span></tt></a>, which can also be <tt class="xref docutils literal"><span class="pre">None</span></tt>
if no exception occurred).  The method&#8217;s return value controls whether an exception
is re-raised: any false value re-raises the exception, and <tt class="xref docutils literal"><span class="pre">True</span></tt> will result
in suppressing it.  You&#8217;ll only rarely want to suppress the exception, because
if you do the author of the code containing the &#8216;<a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a>&#8216; statement will
never realize anything went wrong.</li>
<li>If <em>BLOCK</em> didn&#8217;t raise an exception,  the <a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><tt class="xref py py-meth docutils literal"><span class="pre">__exit__()</span></tt></a> method is still
called, but <em>type</em>, <em>value</em>, and <em>traceback</em> are all <tt class="xref docutils literal"><span class="pre">None</span></tt>.</li>
</ul>
<p>Let&#8217;s think through an example.  I won&#8217;t present detailed code but will only
sketch the methods necessary for a database that supports transactions.</p>
<p>(For people unfamiliar with database terminology: a set of changes to the
database are grouped into a transaction.  Transactions can be either committed,
meaning that all the changes are written into the database, or rolled back,
meaning that the changes are all discarded and the database is unchanged.  See
any database textbook for more information.)</p>
<p>Let&#8217;s assume there&#8217;s an object representing a database connection. Our goal will
be to let the user write code like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">db_connection</span> <span class="o">=</span> <span class="n">DatabaseConnection</span><span class="p">()</span>
<span class="k">with</span> <span class="n">db_connection</span> <span class="k">as</span> <span class="n">cursor</span><span class="p">:</span>
    <span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&#39;insert into ...&#39;</span><span class="p">)</span>
    <span class="n">cursor</span><span class="o">.</span><span class="n">execute</span><span class="p">(</span><span class="s">&#39;delete from ...&#39;</span><span class="p">)</span>
    <span class="c"># ... more operations ...</span>
</pre></div>
</div>
<p>The transaction should be committed if the code in the block runs flawlessly or
rolled back if there&#8217;s an exception. Here&#8217;s the basic interface for
<tt class="xref py py-class docutils literal"><span class="pre">DatabaseConnection</span></tt> that I&#8217;ll assume:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">DatabaseConnection</span><span class="p">:</span>
    <span class="c"># Database interface</span>
    <span class="k">def</span> <span class="nf">cursor</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Returns a cursor object and starts a new transaction&quot;</span>
    <span class="k">def</span> <span class="nf">commit</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Commits current transaction&quot;</span>
    <span class="k">def</span> <span class="nf">rollback</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="s">&quot;Rolls back current transaction&quot;</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="../reference/datamodel.html#object.__enter__" title="object.__enter__"><tt class="xref py py-meth docutils literal"><span class="pre">__enter__()</span></tt></a> method is pretty easy, having only to start a new
transaction.  For this application the resulting cursor object would be a useful
result, so the method will return it.  The user can then add <tt class="docutils literal"><span class="pre">as</span> <span class="pre">cursor</span></tt> to
their &#8216;<a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a>&#8216; statement to bind the cursor to a variable name.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">DatabaseConnection</span><span class="p">:</span>
    <span class="o">...</span>
    <span class="k">def</span> <span class="nf">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="c"># Code to start a new transaction</span>
        <span class="n">cursor</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">cursor</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><tt class="xref py py-meth docutils literal"><span class="pre">__exit__()</span></tt></a> method is the most complicated because it&#8217;s where most of
the work has to be done.  The method has to check if an exception occurred.  If
there was no exception, the transaction is committed.  The transaction is rolled
back if there was an exception.</p>
<p>In the code below, execution will just fall off the end of the function,
returning the default value of <tt class="xref docutils literal"><span class="pre">None</span></tt>.  <tt class="xref docutils literal"><span class="pre">None</span></tt> is false, so the exception
will be re-raised automatically.  If you wished, you could be more explicit and
add a <a class="reference internal" href="../reference/simple_stmts.html#return"><tt class="xref std std-keyword docutils literal"><span class="pre">return</span></tt></a> statement at the marked location.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">DatabaseConnection</span><span class="p">:</span>
    <span class="o">...</span>
    <span class="k">def</span> <span class="nf">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="nb">type</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">tb</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">tb</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
            <span class="c"># No exception, so commit</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c"># Exception occurred, so rollback.</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">rollback</span><span class="p">()</span>
            <span class="c"># return False</span>
</pre></div>
</div>
</div>
<div class="section" id="the-contextlib-module">
<span id="module-contextlib"></span><h3>The contextlib module<a class="headerlink" href="#the-contextlib-module" title="Permalink to this headline">¶</a></h3>
<p>The <a class="reference internal" href="../library/contextlib.html#module-contextlib" title="contextlib: Utilities for with-statement contexts."><tt class="xref py py-mod docutils literal"><span class="pre">contextlib</span></tt></a> module provides some functions and a decorator that
are useful when writing objects for use with the &#8216;<a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a>&#8216; statement.</p>
<p>The decorator is called <tt class="xref py py-func docutils literal"><span class="pre">contextmanager()</span></tt>, and lets you write a single
generator function instead of defining a new class.  The generator should yield
exactly one value.  The code up to the <a class="reference internal" href="../reference/simple_stmts.html#yield"><tt class="xref std std-keyword docutils literal"><span class="pre">yield</span></tt></a> will be executed as the
<a class="reference internal" href="../reference/datamodel.html#object.__enter__" title="object.__enter__"><tt class="xref py py-meth docutils literal"><span class="pre">__enter__()</span></tt></a> method, and the value yielded will be the method&#8217;s return
value that will get bound to the variable in the &#8216;<a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a>&#8216; statement&#8217;s
<a class="reference internal" href="../reference/compound_stmts.html#as"><tt class="xref std std-keyword docutils literal"><span class="pre">as</span></tt></a> clause, if any.  The code after the <a class="reference internal" href="../reference/simple_stmts.html#yield"><tt class="xref std std-keyword docutils literal"><span class="pre">yield</span></tt></a> will be
executed in the <a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><tt class="xref py py-meth docutils literal"><span class="pre">__exit__()</span></tt></a> method.  Any exception raised in the block will
be raised by the <a class="reference internal" href="../reference/simple_stmts.html#yield"><tt class="xref std std-keyword docutils literal"><span class="pre">yield</span></tt></a> statement.</p>
<p>Using this decorator, our database example from the previous section
could be written as:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">contextlib</span> <span class="kn">import</span> <span class="n">contextmanager</span>

<span class="nd">@contextmanager</span>
<span class="k">def</span> <span class="nf">db_transaction</span><span class="p">(</span><span class="n">connection</span><span class="p">):</span>
    <span class="n">cursor</span> <span class="o">=</span> <span class="n">connection</span><span class="o">.</span><span class="n">cursor</span><span class="p">()</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="k">yield</span> <span class="n">cursor</span>
    <span class="k">except</span><span class="p">:</span>
        <span class="n">connection</span><span class="o">.</span><span class="n">rollback</span><span class="p">()</span>
        <span class="k">raise</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">connection</span><span class="o">.</span><span class="n">commit</span><span class="p">()</span>

<span class="n">db</span> <span class="o">=</span> <span class="n">DatabaseConnection</span><span class="p">()</span>
<span class="k">with</span> <span class="n">db_transaction</span><span class="p">(</span><span class="n">db</span><span class="p">)</span> <span class="k">as</span> <span class="n">cursor</span><span class="p">:</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="../library/contextlib.html#module-contextlib" title="contextlib: Utilities for with-statement contexts."><tt class="xref py py-mod docutils literal"><span class="pre">contextlib</span></tt></a> module also has a <tt class="docutils literal"><span class="pre">nested(mgr1,</span> <span class="pre">mgr2,</span> <span class="pre">...)</span></tt> function
that combines a number of context managers so you don&#8217;t need to write nested
&#8216;<a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a>&#8216; statements.  In this example, the single &#8216;<a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a>&#8216;
statement both starts a database transaction and acquires a thread lock:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">lock</span> <span class="o">=</span> <span class="n">threading</span><span class="o">.</span><span class="n">Lock</span><span class="p">()</span>
<span class="k">with</span> <span class="n">nested</span> <span class="p">(</span><span class="n">db_transaction</span><span class="p">(</span><span class="n">db</span><span class="p">),</span> <span class="n">lock</span><span class="p">)</span> <span class="k">as</span> <span class="p">(</span><span class="n">cursor</span><span class="p">,</span> <span class="n">locked</span><span class="p">):</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>Finally, the <tt class="xref py py-func docutils literal"><span class="pre">closing()</span></tt> function returns its argument so that it can be
bound to a variable, and calls the argument&#8217;s <tt class="docutils literal"><span class="pre">.close()</span></tt> method at the end
of the block.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">urllib</span><span class="o">,</span> <span class="nn">sys</span>
<span class="kn">from</span> <span class="nn">contextlib</span> <span class="kn">import</span> <span class="n">closing</span>

<span class="k">with</span> <span class="n">closing</span><span class="p">(</span><span class="n">urllib</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s">&#39;http://www.yahoo.com&#39;</span><span class="p">))</span> <span class="k">as</span> <span class="n">f</span><span class="p">:</span>
    <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
        <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">line</span><span class="p">)</span>
</pre></div>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="docutils">
<dt><span class="target" id="index-3"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0343"><strong>PEP 343</strong></a> - The &#8220;with&#8221; statement</dt>
<dd>PEP written by Guido van Rossum and Nick Coghlan; implemented by Mike Bland,
Guido van Rossum, and Neal Norwitz.  The PEP shows the code generated for a
&#8216;<a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a>&#8216; statement, which can be helpful in learning how the statement
works.</dd>
</dl>
<p class="last">The documentation  for the <a class="reference internal" href="../library/contextlib.html#module-contextlib" title="contextlib: Utilities for with-statement contexts."><tt class="xref py py-mod docutils literal"><span class="pre">contextlib</span></tt></a> module.</p>
</div>
</div>
</div>
<div class="section" id="pep-366-explicit-relative-imports-from-a-main-module">
<span id="pep-0366"></span><h2>PEP 366: Explicit Relative Imports From a Main Module<a class="headerlink" href="#pep-366-explicit-relative-imports-from-a-main-module" title="Permalink to this headline">¶</a></h2>
<p>Python&#8217;s <a class="reference internal" href="../using/cmdline.html#cmdoption-m"><em class="xref std std-option">-m</em></a> switch allows running a module as a script.
When you ran a module that was located inside a package, relative
imports didn&#8217;t work correctly.</p>
<p>The fix for Python 2.6 adds a <tt class="xref py py-attr docutils literal"><span class="pre">__package__</span></tt> attribute to
modules.  When this attribute is present, relative imports will be
relative to the value of this attribute instead of the
<tt class="xref py py-attr docutils literal"><span class="pre">__name__</span></tt> attribute.</p>
<p>PEP 302-style importers can then set <tt class="xref py py-attr docutils literal"><span class="pre">__package__</span></tt> as necessary.
The <a class="reference internal" href="../library/runpy.html#module-runpy" title="runpy: Locate and run Python modules without importing them first."><tt class="xref py py-mod docutils literal"><span class="pre">runpy</span></tt></a> module that implements the <a class="reference internal" href="../using/cmdline.html#cmdoption-m"><em class="xref std std-option">-m</em></a> switch now
does this, so relative imports will now work correctly in scripts
running from inside a package.</p>
</div>
<div class="section" id="pep-370-per-user-site-packages-directory">
<span id="pep-0370"></span><h2>PEP 370: Per-user <tt class="docutils literal"><span class="pre">site-packages</span></tt> Directory<a class="headerlink" href="#pep-370-per-user-site-packages-directory" title="Permalink to this headline">¶</a></h2>
<p>When you run Python, the module search path <tt class="docutils literal"><span class="pre">sys.path</span></tt> usually
includes a directory whose path ends in <tt class="docutils literal"><span class="pre">&quot;site-packages&quot;</span></tt>.  This
directory is intended to hold locally-installed packages available to
all users using a machine or a particular site installation.</p>
<p>Python 2.6 introduces a convention for user-specific site directories.
The directory varies depending on the platform:</p>
<ul class="simple">
<li>Unix and Mac OS X: <tt class="file docutils literal"><span class="pre">~/.local/</span></tt></li>
<li>Windows: <tt class="file docutils literal"><span class="pre">%APPDATA%/Python</span></tt></li>
</ul>
<p>Within this directory, there will be version-specific subdirectories,
such as <tt class="file docutils literal"><span class="pre">lib/python2.6/site-packages</span></tt> on Unix/Mac OS and
<tt class="file docutils literal"><span class="pre">Python26/site-packages</span></tt> on Windows.</p>
<p>If you don&#8217;t like the default directory, it can be overridden by an
environment variable.  <span class="target" id="index-4"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONUSERBASE"><tt class="xref std std-envvar docutils literal"><span class="pre">PYTHONUSERBASE</span></tt></a> sets the root
directory used for all Python versions supporting this feature.  On
Windows, the directory for application-specific data can be changed by
setting the <span class="target" id="index-5"></span><tt class="xref std std-envvar docutils literal"><span class="pre">APPDATA</span></tt> environment variable.  You can also
modify the <tt class="file docutils literal"><span class="pre">site.py</span></tt> file for your Python installation.</p>
<p>The feature can be disabled entirely by running Python with the
<a class="reference internal" href="../using/cmdline.html#cmdoption-s"><em class="xref std std-option">-s</em></a> option or setting the <span class="target" id="index-6"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONNOUSERSITE"><tt class="xref std std-envvar docutils literal"><span class="pre">PYTHONNOUSERSITE</span></tt></a>
environment variable.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><span class="target" id="index-7"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0370"><strong>PEP 370</strong></a> - Per-user <tt class="docutils literal"><span class="pre">site-packages</span></tt> Directory</dt>
<dd>PEP written and implemented by Christian Heimes.</dd>
</dl>
</div>
</div>
<div class="section" id="pep-371-the-multiprocessing-package">
<span id="pep-0371"></span><h2>PEP 371: The <tt class="docutils literal"><span class="pre">multiprocessing</span></tt> Package<a class="headerlink" href="#pep-371-the-multiprocessing-package" title="Permalink to this headline">¶</a></h2>
<p>The new <a class="reference internal" href="../library/multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based &quot;threading&quot; interface."><tt class="xref py py-mod docutils literal"><span class="pre">multiprocessing</span></tt></a> package lets Python programs create new
processes that will perform a computation and return a result to the
parent.  The parent and child processes can communicate using queues
and pipes, synchronize their operations using locks and semaphores,
and can share simple arrays of data.</p>
<p>The <a class="reference internal" href="../library/multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based &quot;threading&quot; interface."><tt class="xref py py-mod docutils literal"><span class="pre">multiprocessing</span></tt></a> module started out as an exact emulation of
the <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Higher-level threading interface."><tt class="xref py py-mod docutils literal"><span class="pre">threading</span></tt></a> module using processes instead of threads.  That
goal was discarded along the path to Python 2.6, but the general
approach of the module is still similar.  The fundamental class
is the <tt class="xref py py-class docutils literal"><span class="pre">Process</span></tt>, which is passed a callable object and
a collection of arguments.  The <tt class="xref py py-meth docutils literal"><span class="pre">start()</span></tt> method
sets the callable running in a subprocess, after which you can call
the <tt class="xref py py-meth docutils literal"><span class="pre">is_alive()</span></tt> method to check whether the subprocess is still running
and the <tt class="xref py py-meth docutils literal"><span class="pre">join()</span></tt> method to wait for the process to exit.</p>
<p>Here&#8217;s a simple example where the subprocess will calculate a
factorial.  The function doing the calculation is written strangely so
that it takes significantly longer when the input argument is a
multiple of 4.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Process</span><span class="p">,</span> <span class="n">Queue</span>


<span class="k">def</span> <span class="nf">factorial</span><span class="p">(</span><span class="n">queue</span><span class="p">,</span> <span class="n">N</span><span class="p">):</span>
    <span class="s">&quot;Compute a factorial.&quot;</span>
    <span class="c"># If N is a multiple of 4, this function will take much longer.</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">N</span> <span class="o">%</span> <span class="mi">4</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
        <span class="n">time</span><span class="o">.</span><span class="n">sleep</span><span class="p">(</span><span class="o">.</span><span class="mo">05</span> <span class="o">*</span> <span class="n">N</span><span class="o">/</span><span class="mi">4</span><span class="p">)</span>

    <span class="c"># Calculate the result</span>
    <span class="n">fact</span> <span class="o">=</span> <span class="il">1L</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">N</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
        <span class="n">fact</span> <span class="o">=</span> <span class="n">fact</span> <span class="o">*</span> <span class="n">i</span>

    <span class="c"># Put the result on the queue</span>
    <span class="n">queue</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">fact</span><span class="p">)</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">queue</span> <span class="o">=</span> <span class="n">Queue</span><span class="p">()</span>

    <span class="n">N</span> <span class="o">=</span> <span class="mi">5</span>

    <span class="n">p</span> <span class="o">=</span> <span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">factorial</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">queue</span><span class="p">,</span> <span class="n">N</span><span class="p">))</span>
    <span class="n">p</span><span class="o">.</span><span class="n">start</span><span class="p">()</span>
    <span class="n">p</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>

    <span class="n">result</span> <span class="o">=</span> <span class="n">queue</span><span class="o">.</span><span class="n">get</span><span class="p">()</span>
    <span class="k">print</span> <span class="s">&#39;Factorial&#39;</span><span class="p">,</span> <span class="n">N</span><span class="p">,</span> <span class="s">&#39;=&#39;</span><span class="p">,</span> <span class="n">result</span>
</pre></div>
</div>
<p>A <a class="reference internal" href="../library/queue.html#module-Queue" title="Queue: A synchronized queue class."><tt class="xref py py-class docutils literal"><span class="pre">Queue</span></tt></a> is used to communicate the result of the factorial.
The <a class="reference internal" href="../library/queue.html#module-Queue" title="Queue: A synchronized queue class."><tt class="xref py py-class docutils literal"><span class="pre">Queue</span></tt></a> object is stored in a global variable.
The child process will use the value of the variable when the child
was created; because it&#8217;s a <a class="reference internal" href="../library/queue.html#module-Queue" title="Queue: A synchronized queue class."><tt class="xref py py-class docutils literal"><span class="pre">Queue</span></tt></a>, parent and child can use
the object to communicate.  (If the parent were to change the value of
the global variable, the child&#8217;s value would be unaffected, and vice
versa.)</p>
<p>Two other classes, <tt class="xref py py-class docutils literal"><span class="pre">Pool</span></tt> and <tt class="xref py py-class docutils literal"><span class="pre">Manager</span></tt>, provide
higher-level interfaces.  <tt class="xref py py-class docutils literal"><span class="pre">Pool</span></tt> will create a fixed number of
worker processes, and requests can then be distributed to the workers
by calling <a class="reference internal" href="../library/functions.html#apply" title="apply"><tt class="xref py py-meth docutils literal"><span class="pre">apply()</span></tt></a> or <tt class="xref py py-meth docutils literal"><span class="pre">apply_async()</span></tt> to add a single request,
and <a class="reference internal" href="../library/functions.html#map" title="map"><tt class="xref py py-meth docutils literal"><span class="pre">map()</span></tt></a> or <tt class="xref py py-meth docutils literal"><span class="pre">map_async()</span></tt> to add a number of
requests.  The following code uses a <tt class="xref py py-class docutils literal"><span class="pre">Pool</span></tt> to spread requests
across 5 worker processes and retrieve a list of results:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Pool</span>

<span class="k">def</span> <span class="nf">factorial</span><span class="p">(</span><span class="n">N</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">):</span>
    <span class="s">&quot;Compute a factorial.&quot;</span>
    <span class="o">...</span>
<span class="n">p</span> <span class="o">=</span> <span class="n">Pool</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
<span class="n">result</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">map</span><span class="p">(</span><span class="n">factorial</span><span class="p">,</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1000</span><span class="p">,</span> <span class="mi">10</span><span class="p">))</span>
<span class="k">for</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">result</span><span class="p">:</span>
    <span class="k">print</span> <span class="n">v</span>
</pre></div>
</div>
<p>This produces the following output:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="mi">1</span>
<span class="mi">39916800</span>
<span class="mi">51090942171709440000</span>
<span class="mi">8222838654177922817725562880000000</span>
<span class="mi">33452526613163807108170062053440751665152000000000</span>
<span class="o">...</span>
</pre></div>
</div>
<p>The other high-level interface, the <tt class="xref py py-class docutils literal"><span class="pre">Manager</span></tt> class, creates a
separate server process that can hold master copies of Python data
structures.  Other processes can then access and modify these data
structures using proxy objects.  The following example creates a
shared dictionary by calling the <a class="reference internal" href="../library/stdtypes.html#dict" title="dict"><tt class="xref py py-meth docutils literal"><span class="pre">dict()</span></tt></a> method; the worker
processes then insert values into the dictionary.  (Locking is not
done for you automatically, which doesn&#8217;t matter in this example.
<tt class="xref py py-class docutils literal"><span class="pre">Manager</span></tt>&#8216;s methods also include <tt class="xref py py-meth docutils literal"><span class="pre">Lock()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">RLock()</span></tt>,
and <tt class="xref py py-meth docutils literal"><span class="pre">Semaphore()</span></tt> to create shared locks.)</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">time</span>
<span class="kn">from</span> <span class="nn">multiprocessing</span> <span class="kn">import</span> <span class="n">Pool</span><span class="p">,</span> <span class="n">Manager</span>

<span class="k">def</span> <span class="nf">factorial</span><span class="p">(</span><span class="n">N</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">):</span>
    <span class="s">&quot;Compute a factorial.&quot;</span>
    <span class="c"># Calculate the result</span>
    <span class="n">fact</span> <span class="o">=</span> <span class="il">1L</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="n">N</span><span class="o">+</span><span class="mi">1</span><span class="p">):</span>
        <span class="n">fact</span> <span class="o">=</span> <span class="n">fact</span> <span class="o">*</span> <span class="n">i</span>

    <span class="c"># Store result in dictionary</span>
    <span class="n">dictionary</span><span class="p">[</span><span class="n">N</span><span class="p">]</span> <span class="o">=</span> <span class="n">fact</span>

<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s">&#39;__main__&#39;</span><span class="p">:</span>
    <span class="n">p</span> <span class="o">=</span> <span class="n">Pool</span><span class="p">(</span><span class="mi">5</span><span class="p">)</span>
    <span class="n">mgr</span> <span class="o">=</span> <span class="n">Manager</span><span class="p">()</span>
    <span class="n">d</span> <span class="o">=</span> <span class="n">mgr</span><span class="o">.</span><span class="n">dict</span><span class="p">()</span>         <span class="c"># Create shared dictionary</span>

    <span class="c"># Run tasks using the pool</span>
    <span class="k">for</span> <span class="n">N</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">1000</span><span class="p">,</span> <span class="mi">10</span><span class="p">):</span>
        <span class="n">p</span><span class="o">.</span><span class="n">apply_async</span><span class="p">(</span><span class="n">factorial</span><span class="p">,</span> <span class="p">(</span><span class="n">N</span><span class="p">,</span> <span class="n">d</span><span class="p">))</span>

    <span class="c"># Mark pool as closed -- no more tasks can be added.</span>
    <span class="n">p</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>

    <span class="c"># Wait for tasks to exit</span>
    <span class="n">p</span><span class="o">.</span><span class="n">join</span><span class="p">()</span>

    <span class="c"># Output results</span>
    <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">d</span><span class="o">.</span><span class="n">items</span><span class="p">()):</span>
        <span class="k">print</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span>
</pre></div>
</div>
<p>This will produce the output:</p>
<div class="highlight-python"><pre>1 1
11 39916800
21 51090942171709440000
31 8222838654177922817725562880000000
41 33452526613163807108170062053440751665152000000000
51 15511187532873822802242430164693032110632597200169861120000...</pre>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<p>The documentation for the <a class="reference internal" href="../library/multiprocessing.html#module-multiprocessing" title="multiprocessing: Process-based &quot;threading&quot; interface."><tt class="xref py py-mod docutils literal"><span class="pre">multiprocessing</span></tt></a> module.</p>
<dl class="last docutils">
<dt><span class="target" id="index-8"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0371"><strong>PEP 371</strong></a> - Addition of the multiprocessing package</dt>
<dd>PEP written by Jesse Noller and Richard Oudkerk;
implemented by Richard Oudkerk and Jesse Noller.</dd>
</dl>
</div>
</div>
<div class="section" id="pep-3101-advanced-string-formatting">
<span id="pep-3101"></span><h2>PEP 3101: Advanced String Formatting<a class="headerlink" href="#pep-3101-advanced-string-formatting" title="Permalink to this headline">¶</a></h2>
<p>In Python 3.0, the <cite>%</cite> operator is supplemented by a more powerful string
formatting method, <a class="reference internal" href="../library/functions.html#format" title="format"><tt class="xref py py-meth docutils literal"><span class="pre">format()</span></tt></a>.  Support for the <a class="reference internal" href="../library/stdtypes.html#str.format" title="str.format"><tt class="xref py py-meth docutils literal"><span class="pre">str.format()</span></tt></a> method
has been backported to Python 2.6.</p>
<p>In 2.6, both 8-bit and Unicode strings have a <cite>.format()</cite> method that
treats the string as a template and takes the arguments to be formatted.
The formatting template uses curly brackets (<cite>{</cite>, <cite>}</cite>) as special characters:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># Substitute positional argument 0 into the string.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&quot;User ID: {0}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s">&quot;root&quot;</span><span class="p">)</span>
<span class="go">&#39;User ID: root&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># Use the named keyword arguments</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&quot;User ID: {uid}   Last seen: {last_login}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span>
<span class="gp">... </span>   <span class="n">uid</span><span class="o">=</span><span class="s">&quot;root&quot;</span><span class="p">,</span>
<span class="gp">... </span>   <span class="n">last_login</span> <span class="o">=</span> <span class="s">&quot;5 Mar 2008 07:20&quot;</span><span class="p">)</span>
<span class="go">&#39;User ID: root   Last seen: 5 Mar 2008 07:20&#39;</span>
</pre></div>
</div>
<p>Curly brackets can be escaped by doubling them:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">&quot;Empty dict: {{}}&quot;</span><span class="o">.</span><span class="n">format</span><span class="p">()</span>
<span class="go">&quot;Empty dict: {}&quot;</span>
</pre></div>
</div>
<p>Field names can be integers indicating positional arguments, such as
<tt class="docutils literal"><span class="pre">{0}</span></tt>, <tt class="docutils literal"><span class="pre">{1}</span></tt>, etc. or names of keyword arguments.  You can also
supply compound field names that read attributes or access dictionary keys:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">sys</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="s">&#39;Platform: {0.platform}</span><span class="se">\n</span><span class="s">Python version: {0.version}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">sys</span><span class="p">)</span>
<span class="go">Platform: darwin</span>
<span class="go">Python version: 2.6a1+ (trunk:61261M, Mar  5 2008, 20:29:41)</span>
<span class="go">[GCC 4.0.1 (Apple Computer, Inc. build 5367)]&#39;</span>

<span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">mimetypes</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;Content-type: {0[.mp4]}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">mimetypes</span><span class="o">.</span><span class="n">types_map</span><span class="p">)</span>
<span class="go">&#39;Content-type: video/mp4&#39;</span>
</pre></div>
</div>
<p>Note that when using dictionary-style notation such as <tt class="docutils literal"><span class="pre">[.mp4]</span></tt>, you
don&#8217;t need to put any quotation marks around the string; it will look
up the value using <tt class="docutils literal"><span class="pre">.mp4</span></tt> as the key.  Strings beginning with a
number will be converted to an integer.  You can&#8217;t write more
complicated expressions inside a format string.</p>
<p>So far we&#8217;ve shown how to specify which field to substitute into the
resulting string.  The precise formatting used is also controllable by
adding a colon followed by a format specifier.  For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># Field 0: left justify, pad to 15 characters</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># Field 1: right justify, pad to 6 characters</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fmt</span> <span class="o">=</span> <span class="s">&#39;{0:15} ${1:&gt;6}&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fmt</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s">&#39;Registration&#39;</span><span class="p">,</span> <span class="mi">35</span><span class="p">)</span>
<span class="go">&#39;Registration    $    35&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fmt</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s">&#39;Tutorial&#39;</span><span class="p">,</span> <span class="mi">50</span><span class="p">)</span>
<span class="go">&#39;Tutorial        $    50&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fmt</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s">&#39;Banquet&#39;</span><span class="p">,</span> <span class="mi">125</span><span class="p">)</span>
<span class="go">&#39;Banquet         $   125&#39;</span>
</pre></div>
</div>
<p>Format specifiers can reference other fields through nesting:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">fmt</span> <span class="o">=</span> <span class="s">&#39;{0:{1}}&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">width</span> <span class="o">=</span> <span class="mi">15</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fmt</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s">&#39;Invoice #1234&#39;</span><span class="p">,</span> <span class="n">width</span><span class="p">)</span>
<span class="go">&#39;Invoice #1234  &#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">width</span> <span class="o">=</span> <span class="mi">35</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">fmt</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="s">&#39;Invoice #1234&#39;</span><span class="p">,</span> <span class="n">width</span><span class="p">)</span>
<span class="go">&#39;Invoice #1234                      &#39;</span>
</pre></div>
</div>
<p>The alignment of a field within the desired width can be specified:</p>
<table border="1" class="docutils">
<colgroup>
<col width="27%" />
<col width="73%" />
</colgroup>
<thead valign="bottom">
<tr><th class="head">Character</th>
<th class="head">Effect</th>
</tr>
</thead>
<tbody valign="top">
<tr><td>&lt; (default)</td>
<td>Left-align</td>
</tr>
<tr><td>&gt;</td>
<td>Right-align</td>
</tr>
<tr><td>^</td>
<td>Center</td>
</tr>
<tr><td>=</td>
<td>(For numeric types only) Pad after the sign.</td>
</tr>
</tbody>
</table>
<p>Format specifiers can also include a presentation type, which
controls how the value is formatted.  For example, floating-point numbers
can be formatted as a general number or in exponential notation:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;{0:g}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mf">3.75</span><span class="p">)</span>
<span class="go">&#39;3.75&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="s">&#39;{0:e}&#39;</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="mf">3.75</span><span class="p">)</span>
<span class="go">&#39;3.750000e+00&#39;</span>
</pre></div>
</div>
<p>A variety of presentation types are available.  Consult the 2.6
documentation for a <a class="reference internal" href="../library/string.html#formatstrings"><em>complete list</em></a>; here&#8217;s a sample:</p>
<table border="1" class="docutils">
<colgroup>
<col width="6%" />
<col width="94%" />
</colgroup>
<tbody valign="top">
<tr><td><tt class="docutils literal"><span class="pre">b</span></tt></td>
<td>Binary. Outputs the number in base 2.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">c</span></tt></td>
<td>Character. Converts the integer to the corresponding Unicode character
before printing.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">d</span></tt></td>
<td>Decimal Integer. Outputs the number in base 10.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">o</span></tt></td>
<td>Octal format. Outputs the number in base 8.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">x</span></tt></td>
<td>Hex format. Outputs the number in base 16, using lower-case letters for
the digits above 9.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">e</span></tt></td>
<td>Exponent notation. Prints the number in scientific notation using the
letter &#8216;e&#8217; to indicate the exponent.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">g</span></tt></td>
<td>General format. This prints the number as a fixed-point number, unless
the number is too large, in which case it switches to &#8216;e&#8217; exponent
notation.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">n</span></tt></td>
<td>Number. This is the same as &#8216;g&#8217; (for floats) or &#8216;d&#8217; (for integers),
except that it uses the current locale setting to insert the appropriate
number separator characters.</td>
</tr>
<tr><td><tt class="docutils literal"><span class="pre">%</span></tt></td>
<td>Percentage. Multiplies the number by 100 and displays in fixed (&#8216;f&#8217;)
format, followed by a percent sign.</td>
</tr>
</tbody>
</table>
<p>Classes and types can define a <tt class="xref py py-meth docutils literal"><span class="pre">__format__()</span></tt> method to control how they&#8217;re
formatted.  It receives a single argument, the format specifier:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">__format__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">format_spec</span><span class="p">):</span>
    <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">format_spec</span><span class="p">,</span> <span class="nb">unicode</span><span class="p">):</span>
        <span class="k">return</span> <span class="nb">unicode</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="nb">str</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
</pre></div>
</div>
<p>There&#8217;s also a <a class="reference internal" href="../library/functions.html#format" title="format"><tt class="xref py py-func docutils literal"><span class="pre">format()</span></tt></a> builtin that will format a single
value.  It calls the type&#8217;s <tt class="xref py py-meth docutils literal"><span class="pre">__format__()</span></tt> method with the
provided specifier:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">format</span><span class="p">(</span><span class="mf">75.6564</span><span class="p">,</span> <span class="s">&#39;.2f&#39;</span><span class="p">)</span>
<span class="go">&#39;75.66&#39;</span>
</pre></div>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><a class="reference internal" href="../library/string.html#formatstrings"><em>Format String Syntax</em></a></dt>
<dd>The reference documentation for format fields.</dd>
<dt><span class="target" id="index-9"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3101"><strong>PEP 3101</strong></a> - Advanced String Formatting</dt>
<dd>PEP written by Talin. Implemented by Eric Smith.</dd>
</dl>
</div>
</div>
<div class="section" id="pep-3105-print-as-a-function">
<span id="pep-3105"></span><h2>PEP 3105: <tt class="docutils literal"><span class="pre">print</span></tt> As a Function<a class="headerlink" href="#pep-3105-print-as-a-function" title="Permalink to this headline">¶</a></h2>
<p>The <tt class="docutils literal"><span class="pre">print</span></tt> statement becomes the <a class="reference internal" href="../library/functions.html#print" title="print"><tt class="xref py py-func docutils literal"><span class="pre">print()</span></tt></a> function in Python 3.0.
Making <a class="reference internal" href="../library/functions.html#print" title="print"><tt class="xref py py-func docutils literal"><span class="pre">print()</span></tt></a> a function makes it possible to replace the function
by doing <tt class="docutils literal"><span class="pre">def</span> <span class="pre">print(...)</span></tt> or importing a new function from somewhere else.</p>
<p>Python 2.6 has a <tt class="docutils literal"><span class="pre">__future__</span></tt> import that removes <tt class="docutils literal"><span class="pre">print</span></tt> as language
syntax, letting you use the functional form instead.  For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">print_function</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span><span class="p">(</span><span class="s">&#39;# of entries&#39;</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="n">dictionary</span><span class="p">),</span> <span class="nb">file</span><span class="o">=</span><span class="n">sys</span><span class="o">.</span><span class="n">stderr</span><span class="p">)</span>
</pre></div>
</div>
<p>The signature of the new function is:</p>
<div class="highlight-python"><pre>def print(*args, sep=' ', end='\n', file=None)</pre>
</div>
<p>The parameters are:</p>
<blockquote>
<div><ul class="simple">
<li><em>args</em>: positional arguments whose values will be printed out.</li>
<li><em>sep</em>: the separator, which will be printed between arguments.</li>
<li><em>end</em>: the ending text, which will be printed after all of the
arguments have been output.</li>
<li><em>file</em>: the file object to which the output will be sent.</li>
</ul>
</div></blockquote>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><span class="target" id="index-10"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3105"><strong>PEP 3105</strong></a> - Make print a function</dt>
<dd>PEP written by Georg Brandl.</dd>
</dl>
</div>
</div>
<div class="section" id="pep-3110-exception-handling-changes">
<span id="pep-3110"></span><h2>PEP 3110: Exception-Handling Changes<a class="headerlink" href="#pep-3110-exception-handling-changes" title="Permalink to this headline">¶</a></h2>
<p>One error that Python programmers occasionally make
is writing the following code:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span>
    <span class="o">...</span>
<span class="k">except</span> <span class="ne">TypeError</span><span class="p">,</span> <span class="ne">ValueError</span><span class="p">:</span>  <span class="c"># Wrong!</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>The author is probably trying to catch both <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> and
<a class="reference internal" href="../library/exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> exceptions, but this code actually does something
different: it will catch <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a> and bind the resulting
exception object to the local name <tt class="docutils literal"><span class="pre">&quot;ValueError&quot;</span></tt>.  The
<a class="reference internal" href="../library/exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> exception will not be caught at all.  The correct
code specifies a tuple of exceptions:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span>
    <span class="o">...</span>
<span class="k">except</span> <span class="p">(</span><span class="ne">TypeError</span><span class="p">,</span> <span class="ne">ValueError</span><span class="p">):</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>This error happens because the use of the comma here is ambiguous:
does it indicate two different nodes in the parse tree, or a single
node that&#8217;s a tuple?</p>
<p>Python 3.0 makes this unambiguous by replacing the comma with the word
&#8220;as&#8221;.  To catch an exception and store the exception object in the
variable <tt class="docutils literal"><span class="pre">exc</span></tt>, you must write:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">try</span><span class="p">:</span>
    <span class="o">...</span>
<span class="k">except</span> <span class="ne">TypeError</span> <span class="k">as</span> <span class="n">exc</span><span class="p">:</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>Python 3.0 will only support the use of &#8220;as&#8221;, and therefore interprets
the first example as catching two different exceptions.  Python 2.6
supports both the comma and &#8220;as&#8221;, so existing code will continue to
work.  We therefore suggest using &#8220;as&#8221; when writing new Python code
that will only be executed with 2.6.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><span class="target" id="index-11"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3110"><strong>PEP 3110</strong></a> - Catching Exceptions in Python 3000</dt>
<dd>PEP written and implemented by Collin Winter.</dd>
</dl>
</div>
</div>
<div class="section" id="pep-3112-byte-literals">
<span id="pep-3112"></span><h2>PEP 3112: Byte Literals<a class="headerlink" href="#pep-3112-byte-literals" title="Permalink to this headline">¶</a></h2>
<p>Python 3.0 adopts Unicode as the language&#8217;s fundamental string type and
denotes 8-bit literals differently, either as <tt class="docutils literal"><span class="pre">b'string'</span></tt>
or using a <tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt> constructor.  For future compatibility,
Python 2.6 adds <tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt> as a synonym for the <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a> type,
and it also supports the <tt class="docutils literal"><span class="pre">b''</span></tt> notation.</p>
<p>The 2.6 <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a> differs from 3.0&#8217;s <tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt> type in various
ways; most notably, the constructor is completely different.  In 3.0,
<tt class="docutils literal"><span class="pre">bytes([65,</span> <span class="pre">66,</span> <span class="pre">67])</span></tt> is 3 elements long, containing the bytes
representing <tt class="docutils literal"><span class="pre">ABC</span></tt>; in 2.6, <tt class="docutils literal"><span class="pre">bytes([65,</span> <span class="pre">66,</span> <span class="pre">67])</span></tt> returns the
12-byte string representing the <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> of the list.</p>
<p>The primary use of <tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt> in 2.6 will be to write tests of
object type such as <tt class="docutils literal"><span class="pre">isinstance(x,</span> <span class="pre">bytes)</span></tt>.  This will help the 2to3
converter, which can&#8217;t tell whether 2.x code intends strings to
contain either characters or 8-bit bytes; you can now
use either <tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt> or <a class="reference internal" href="../library/functions.html#str" title="str"><tt class="xref py py-class docutils literal"><span class="pre">str</span></tt></a> to represent your intention
exactly, and the resulting code will also be correct in Python 3.0.</p>
<p>There&#8217;s also a <tt class="docutils literal"><span class="pre">__future__</span></tt> import that causes all string literals
to become Unicode strings.  This means that <tt class="docutils literal"><span class="pre">\u</span></tt> escape sequences
can be used to include Unicode characters:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">unicode_literals</span>

<span class="n">s</span> <span class="o">=</span> <span class="p">(</span><span class="s">&#39;</span><span class="se">\u751f\u3080\u304e\u3000\u751f\u3054</span><span class="s">&#39;</span>
     <span class="s">&#39;</span><span class="se">\u3081\u3000\u751f\u305f\u307e\u3054</span><span class="s">&#39;</span><span class="p">)</span>

<span class="k">print</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>               <span class="c"># 12 Unicode characters</span>
</pre></div>
</div>
<p>At the C level, Python 3.0 will rename the existing 8-bit
string type, called <a class="reference internal" href="../c-api/string.html#PyStringObject" title="PyStringObject"><tt class="xref c c-type docutils literal"><span class="pre">PyStringObject</span></tt></a> in Python 2.x,
to <tt class="xref c c-type docutils literal"><span class="pre">PyBytesObject</span></tt>.  Python 2.6 uses <tt class="docutils literal"><span class="pre">#define</span></tt>
to support using the names <tt class="xref c c-func docutils literal"><span class="pre">PyBytesObject()</span></tt>,
<tt class="xref c c-func docutils literal"><span class="pre">PyBytes_Check()</span></tt>, <tt class="xref c c-func docutils literal"><span class="pre">PyBytes_FromStringAndSize()</span></tt>,
and all the other functions and macros used with strings.</p>
<p>Instances of the <tt class="xref py py-class docutils literal"><span class="pre">bytes</span></tt> type are immutable just
as strings are.  A new <a class="reference internal" href="../library/functions.html#bytearray" title="bytearray"><tt class="xref py py-class docutils literal"><span class="pre">bytearray</span></tt></a> type stores a mutable
sequence of bytes:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">bytearray</span><span class="p">([</span><span class="mi">65</span><span class="p">,</span> <span class="mi">66</span><span class="p">,</span> <span class="mi">67</span><span class="p">])</span>
<span class="go">bytearray(b&#39;ABC&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="nb">bytearray</span><span class="p">(</span><span class="s">u&#39;</span><span class="se">\u21ef\u3244</span><span class="s">&#39;</span><span class="p">,</span> <span class="s">&#39;utf-8&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span>
<span class="go">bytearray(b&#39;\xe2\x87\xaf\xe3\x89\x84&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;</span><span class="se">\xe3</span><span class="s">&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span>
<span class="go">bytearray(b&#39;\xe3\x87\xaf\xe3\x89\x84&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">unicode</span><span class="p">(</span><span class="nb">str</span><span class="p">(</span><span class="n">b</span><span class="p">),</span> <span class="s">&#39;utf-8&#39;</span><span class="p">)</span>
<span class="go">u&#39;\u31ef \u3244&#39;</span>
</pre></div>
</div>
<p>Byte arrays support most of the methods of string types, such as
<tt class="xref py py-meth docutils literal"><span class="pre">startswith()</span></tt>/<tt class="xref py py-meth docutils literal"><span class="pre">endswith()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">find()</span></tt>/<tt class="xref py py-meth docutils literal"><span class="pre">rfind()</span></tt>,
and some of the methods of lists, such as <tt class="xref py py-meth docutils literal"><span class="pre">append()</span></tt>,
<tt class="xref py py-meth docutils literal"><span class="pre">pop()</span></tt>,  and <tt class="xref py py-meth docutils literal"><span class="pre">reverse()</span></tt>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="nb">bytearray</span><span class="p">(</span><span class="s">&#39;ABC&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s">&#39;d&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">ord</span><span class="p">(</span><span class="s">&#39;e&#39;</span><span class="p">))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span>
<span class="go">bytearray(b&#39;ABCde&#39;)</span>
</pre></div>
</div>
<p>There&#8217;s also a corresponding C API, with
<a class="reference internal" href="../c-api/bytearray.html#PyByteArray_FromObject" title="PyByteArray_FromObject"><tt class="xref c c-func docutils literal"><span class="pre">PyByteArray_FromObject()</span></tt></a>,
<a class="reference internal" href="../c-api/bytearray.html#PyByteArray_FromStringAndSize" title="PyByteArray_FromStringAndSize"><tt class="xref c c-func docutils literal"><span class="pre">PyByteArray_FromStringAndSize()</span></tt></a>,
and various other functions.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><span class="target" id="index-12"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3112"><strong>PEP 3112</strong></a> - Bytes literals in Python 3000</dt>
<dd>PEP written by Jason Orendorff; backported to 2.6 by Christian Heimes.</dd>
</dl>
</div>
</div>
<div class="section" id="pep-3116-new-i-o-library">
<span id="pep-3116"></span><h2>PEP 3116: New I/O Library<a class="headerlink" href="#pep-3116-new-i-o-library" title="Permalink to this headline">¶</a></h2>
<p>Python&#8217;s built-in file objects support a number of methods, but
file-like objects don&#8217;t necessarily support all of them.  Objects that
imitate files usually support <tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">write()</span></tt>, but they
may not support <a class="reference internal" href="../library/readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt></a>, for example.  Python 3.0 introduces
a layered I/O library in the <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><tt class="xref py py-mod docutils literal"><span class="pre">io</span></tt></a> module that separates buffering
and text-handling features from the fundamental read and write
operations.</p>
<p>There are three levels of abstract base classes provided by
the <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><tt class="xref py py-mod docutils literal"><span class="pre">io</span></tt></a> module:</p>
<ul>
<li><p class="first"><tt class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></tt> defines raw I/O operations: <tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt>,
<tt class="xref py py-meth docutils literal"><span class="pre">readinto()</span></tt>,
<tt class="xref py py-meth docutils literal"><span class="pre">write()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">seek()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">tell()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">truncate()</span></tt>,
and <tt class="xref py py-meth docutils literal"><span class="pre">close()</span></tt>.
Most of the methods of this class will often map to a single system call.
There are also <tt class="xref py py-meth docutils literal"><span class="pre">readable()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">writable()</span></tt>, and <tt class="xref py py-meth docutils literal"><span class="pre">seekable()</span></tt>
methods for determining what operations a given object will allow.</p>
<p>Python 3.0 has concrete implementations of this class for files and
sockets, but Python 2.6 hasn&#8217;t restructured its file and socket objects
in this way.</p>
</li>
<li><p class="first"><tt class="xref py py-class docutils literal"><span class="pre">BufferedIOBase</span></tt> is an abstract base class that
buffers data in memory to reduce the number of
system calls used, making I/O processing more efficient.
It supports all of the methods of <tt class="xref py py-class docutils literal"><span class="pre">RawIOBase</span></tt>,
and adds a <tt class="xref py py-attr docutils literal"><span class="pre">raw</span></tt> attribute holding the underlying raw object.</p>
<p>There are five concrete classes implementing this ABC.
<tt class="xref py py-class docutils literal"><span class="pre">BufferedWriter</span></tt> and <tt class="xref py py-class docutils literal"><span class="pre">BufferedReader</span></tt> are for objects
that support write-only or read-only usage that have a <tt class="xref py py-meth docutils literal"><span class="pre">seek()</span></tt>
method for random access.  <tt class="xref py py-class docutils literal"><span class="pre">BufferedRandom</span></tt> objects support
read and write access upon the same underlying stream, and
<tt class="xref py py-class docutils literal"><span class="pre">BufferedRWPair</span></tt> is for objects such as TTYs that have both
read and write operations acting upon unconnected streams of data.
The <tt class="xref py py-class docutils literal"><span class="pre">BytesIO</span></tt> class supports reading, writing, and seeking
over an in-memory buffer.</p>
</li>
<li id="index-13"><p class="first"><tt class="xref py py-class docutils literal"><span class="pre">TextIOBase</span></tt>: Provides functions for reading and writing
strings (remember, strings will be Unicode in Python 3.0),
and supporting <a class="reference internal" href="../glossary.html#term-universal-newlines"><em class="xref std std-term">universal newlines</em></a>.  <tt class="xref py py-class docutils literal"><span class="pre">TextIOBase</span></tt> defines
the <a class="reference internal" href="../library/readline.html#module-readline" title="readline: GNU readline support for Python. (Unix)"><tt class="xref py py-meth docutils literal"><span class="pre">readline()</span></tt></a> method and supports iteration upon
objects.</p>
<p>There are two concrete implementations.  <tt class="xref py py-class docutils literal"><span class="pre">TextIOWrapper</span></tt>
wraps a buffered I/O object, supporting all of the methods for
text I/O and adding a <a class="reference internal" href="../library/functions.html#buffer" title="buffer"><tt class="xref py py-attr docutils literal"><span class="pre">buffer</span></tt></a> attribute for access
to the underlying object.  <a class="reference internal" href="../library/stringio.html#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-class docutils literal"><span class="pre">StringIO</span></tt></a> simply buffers
everything in memory without ever writing anything to disk.</p>
<p>(In Python 2.6, <a class="reference internal" href="../library/io.html#io.StringIO" title="io.StringIO"><tt class="xref py py-class docutils literal"><span class="pre">io.StringIO</span></tt></a> is implemented in
pure Python, so it&#8217;s pretty slow.   You should therefore stick with the
existing <a class="reference internal" href="../library/stringio.html#module-StringIO" title="StringIO: Read and write strings as if they were files."><tt class="xref py py-mod docutils literal"><span class="pre">StringIO</span></tt></a> module or <a class="reference internal" href="../library/stringio.html#module-cStringIO" title="cStringIO: Faster version of StringIO, but not subclassable."><tt class="xref py py-mod docutils literal"><span class="pre">cStringIO</span></tt></a> for now.  At some
point Python 3.0&#8217;s <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><tt class="xref py py-mod docutils literal"><span class="pre">io</span></tt></a> module will be rewritten into C for speed,
and perhaps the C implementation will be  backported to the 2.x releases.)</p>
</li>
</ul>
<p>In Python 2.6, the underlying implementations haven&#8217;t been
restructured to build on top of the <a class="reference internal" href="../library/io.html#module-io" title="io: Core tools for working with streams."><tt class="xref py py-mod docutils literal"><span class="pre">io</span></tt></a> module&#8217;s classes.  The
module is being provided to make it easier to write code that&#8217;s
forward-compatible with 3.0, and to save developers the effort of writing
their own implementations of buffering and text I/O.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><span class="target" id="index-14"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3116"><strong>PEP 3116</strong></a> - New I/O</dt>
<dd>PEP written by Daniel Stutzbach, Mike Verdone, and Guido van Rossum.
Code by Guido van Rossum, Georg Brandl, Walter Doerwald,
Jeremy Hylton, Martin von Loewis, Tony Lownds, and others.</dd>
</dl>
</div>
</div>
<div class="section" id="pep-3118-revised-buffer-protocol">
<span id="pep-3118"></span><h2>PEP 3118: Revised Buffer Protocol<a class="headerlink" href="#pep-3118-revised-buffer-protocol" title="Permalink to this headline">¶</a></h2>
<p>The buffer protocol is a C-level API that lets Python types
exchange pointers into their internal representations.  A
memory-mapped file can be viewed as a buffer of characters, for
example, and this lets another module such as <a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><tt class="xref py py-mod docutils literal"><span class="pre">re</span></tt></a>
treat memory-mapped files as a string of characters to be searched.</p>
<p>The primary users of the buffer protocol are numeric-processing
packages such as NumPy, which expose the internal representation
of arrays so that callers can write data directly into an array instead
of going through a slower API.  This PEP updates the buffer protocol in light of experience
from NumPy development, adding a number of new features
such as indicating the shape of an array or locking a memory region.</p>
<p>The most important new C API function is
<tt class="docutils literal"><span class="pre">PyObject_GetBuffer(PyObject</span> <span class="pre">*obj,</span> <span class="pre">Py_buffer</span> <span class="pre">*view,</span> <span class="pre">int</span> <span class="pre">flags)</span></tt>, which
takes an object and a set of flags, and fills in the
<tt class="docutils literal"><span class="pre">Py_buffer</span></tt> structure with information
about the object&#8217;s memory representation.  Objects
can use this operation to lock memory in place
while an external caller could be modifying the contents,
so there&#8217;s a corresponding <tt class="docutils literal"><span class="pre">PyBuffer_Release(Py_buffer</span> <span class="pre">*view)</span></tt> to
indicate that the external caller is done.</p>
<p>The <em>flags</em> argument to <a class="reference internal" href="../c-api/buffer.html#PyObject_GetBuffer" title="PyObject_GetBuffer"><tt class="xref c c-func docutils literal"><span class="pre">PyObject_GetBuffer()</span></tt></a> specifies
constraints upon the memory returned.  Some examples are:</p>
<blockquote>
<div><ul class="simple">
<li><tt class="xref py py-const docutils literal"><span class="pre">PyBUF_WRITABLE</span></tt> indicates that the memory must be writable.</li>
<li><tt class="xref py py-const docutils literal"><span class="pre">PyBUF_LOCK</span></tt> requests a read-only or exclusive lock on the memory.</li>
<li><tt class="xref py py-const docutils literal"><span class="pre">PyBUF_C_CONTIGUOUS</span></tt> and <tt class="xref py py-const docutils literal"><span class="pre">PyBUF_F_CONTIGUOUS</span></tt>
requests a C-contiguous (last dimension varies the fastest) or
Fortran-contiguous (first dimension varies the fastest) array layout.</li>
</ul>
</div></blockquote>
<p>Two new argument codes for <a class="reference internal" href="../c-api/arg.html#PyArg_ParseTuple" title="PyArg_ParseTuple"><tt class="xref c c-func docutils literal"><span class="pre">PyArg_ParseTuple()</span></tt></a>,
<tt class="docutils literal"><span class="pre">s*</span></tt> and <tt class="docutils literal"><span class="pre">z*</span></tt>, return locked buffer objects for a parameter.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><span class="target" id="index-15"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3118"><strong>PEP 3118</strong></a> - Revising the buffer protocol</dt>
<dd>PEP written by Travis Oliphant and Carl Banks; implemented by
Travis Oliphant.</dd>
</dl>
</div>
</div>
<div class="section" id="pep-3119-abstract-base-classes">
<span id="pep-3119"></span><h2>PEP 3119: Abstract Base Classes<a class="headerlink" href="#pep-3119-abstract-base-classes" title="Permalink to this headline">¶</a></h2>
<p>Some object-oriented languages such as Java support interfaces,
declaring that a class has a given set of methods or supports a given
access protocol.  Abstract Base Classes (or ABCs) are an equivalent
feature for Python. The ABC support consists of an <a class="reference internal" href="../library/abc.html#module-abc" title="abc: Abstract base classes according to PEP 3119."><tt class="xref py py-mod docutils literal"><span class="pre">abc</span></tt></a> module
containing a metaclass called <tt class="xref py py-class docutils literal"><span class="pre">ABCMeta</span></tt>, special handling of
this metaclass by the <a class="reference internal" href="../library/functions.html#isinstance" title="isinstance"><tt class="xref py py-func docutils literal"><span class="pre">isinstance()</span></tt></a> and <a class="reference internal" href="../library/functions.html#issubclass" title="issubclass"><tt class="xref py py-func docutils literal"><span class="pre">issubclass()</span></tt></a>
builtins, and a collection of basic ABCs that the Python developers
think will be widely useful.  Future versions of Python will probably
add more ABCs.</p>
<p>Let&#8217;s say you have a particular class and wish to know whether it supports
dictionary-style access.  The phrase &#8220;dictionary-style&#8221; is vague, however.
It probably means that accessing items with <tt class="docutils literal"><span class="pre">obj[1]</span></tt> works.
Does it imply that setting items with <tt class="docutils literal"><span class="pre">obj[2]</span> <span class="pre">=</span> <span class="pre">value</span></tt> works?
Or that the object will have <tt class="xref py py-meth docutils literal"><span class="pre">keys()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">values()</span></tt>, and <tt class="xref py py-meth docutils literal"><span class="pre">items()</span></tt>
methods?  What about the iterative variants  such as <tt class="xref py py-meth docutils literal"><span class="pre">iterkeys()</span></tt>?  <a class="reference internal" href="../library/copy.html#module-copy" title="copy: Shallow and deep copy operations."><tt class="xref py py-meth docutils literal"><span class="pre">copy()</span></tt></a>
and <tt class="xref py py-meth docutils literal"><span class="pre">update()</span></tt>?  Iterating over the object with <a class="reference internal" href="../library/functions.html#iter" title="iter"><tt class="xref py py-func docutils literal"><span class="pre">iter()</span></tt></a>?</p>
<p>The Python 2.6 <a class="reference internal" href="../library/collections.html#module-collections" title="collections: High-performance datatypes"><tt class="xref py py-mod docutils literal"><span class="pre">collections</span></tt></a> module includes a number of
different ABCs that represent these distinctions.  <tt class="xref py py-class docutils literal"><span class="pre">Iterable</span></tt>
indicates that a class defines <a class="reference internal" href="../reference/datamodel.html#object.__iter__" title="object.__iter__"><tt class="xref py py-meth docutils literal"><span class="pre">__iter__()</span></tt></a>, and
<tt class="xref py py-class docutils literal"><span class="pre">Container</span></tt> means the class defines a <a class="reference internal" href="../reference/datamodel.html#object.__contains__" title="object.__contains__"><tt class="xref py py-meth docutils literal"><span class="pre">__contains__()</span></tt></a>
method and therefore supports <tt class="docutils literal"><span class="pre">x</span> <span class="pre">in</span> <span class="pre">y</span></tt> expressions.  The basic
dictionary interface of getting items, setting items, and
<tt class="xref py py-meth docutils literal"><span class="pre">keys()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">values()</span></tt>, and <tt class="xref py py-meth docutils literal"><span class="pre">items()</span></tt>, is defined by the
<tt class="xref py py-class docutils literal"><span class="pre">MutableMapping</span></tt> ABC.</p>
<p>You can derive your own classes from a particular ABC
to indicate they support that ABC&#8217;s interface:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">collections</span>

<span class="k">class</span> <span class="nc">Storage</span><span class="p">(</span><span class="n">collections</span><span class="o">.</span><span class="n">MutableMapping</span><span class="p">):</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>Alternatively, you could write the class without deriving from
the desired ABC and instead register the class by
calling the ABC&#8217;s <tt class="xref py py-meth docutils literal"><span class="pre">register()</span></tt> method:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">collections</span>

<span class="k">class</span> <span class="nc">Storage</span><span class="p">:</span>
    <span class="o">...</span>

<span class="n">collections</span><span class="o">.</span><span class="n">MutableMapping</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="n">Storage</span><span class="p">)</span>
</pre></div>
</div>
<p>For classes that you write, deriving from the ABC is probably clearer.
The <tt class="xref py py-meth docutils literal"><span class="pre">register()</span></tt>  method is useful when you&#8217;ve written a new
ABC that can describe an existing type or class, or if you want
to declare that some third-party class implements an ABC.
For example, if you defined a <tt class="xref py py-class docutils literal"><span class="pre">PrintableType</span></tt> ABC,
it&#8217;s legal to do:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Register Python&#39;s types</span>
<span class="n">PrintableType</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="nb">int</span><span class="p">)</span>
<span class="n">PrintableType</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="nb">float</span><span class="p">)</span>
<span class="n">PrintableType</span><span class="o">.</span><span class="n">register</span><span class="p">(</span><span class="nb">str</span><span class="p">)</span>
</pre></div>
</div>
<p>Classes should obey the semantics specified by an ABC, but
Python can&#8217;t check this; it&#8217;s up to the class author to
understand the ABC&#8217;s requirements and to implement the code accordingly.</p>
<p>To check whether an object supports a particular interface, you can
now write:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="n">d</span><span class="p">):</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">d</span><span class="p">,</span> <span class="n">collections</span><span class="o">.</span><span class="n">MutableMapping</span><span class="p">):</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s">&quot;Mapping object expected, not </span><span class="si">%r</span><span class="s">&quot;</span> <span class="o">%</span> <span class="n">d</span><span class="p">)</span>
</pre></div>
</div>
<p>Don&#8217;t feel that you must now begin writing lots of checks as in the
above example.  Python has a strong tradition of duck-typing, where
explicit type-checking is never done and code simply calls methods on
an object, trusting that those methods will be there and raising an
exception if they aren&#8217;t.  Be judicious in checking for ABCs and only
do it where it&#8217;s absolutely necessary.</p>
<p>You can write your own ABCs by using <tt class="docutils literal"><span class="pre">abc.ABCMeta</span></tt> as the
metaclass in a class definition:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">abc</span> <span class="kn">import</span> <span class="n">ABCMeta</span><span class="p">,</span> <span class="n">abstractmethod</span>

<span class="k">class</span> <span class="nc">Drawable</span><span class="p">():</span>
    <span class="n">__metaclass__</span> <span class="o">=</span> <span class="n">ABCMeta</span>

    <span class="nd">@abstractmethod</span>
    <span class="k">def</span> <span class="nf">draw</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mf">1.0</span><span class="p">):</span>
        <span class="k">pass</span>

    <span class="k">def</span> <span class="nf">draw_doubled</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">draw</span><span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">scale</span><span class="o">=</span><span class="mf">2.0</span><span class="p">)</span>


<span class="k">class</span> <span class="nc">Square</span><span class="p">(</span><span class="n">Drawable</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">draw</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">x</span><span class="p">,</span> <span class="n">y</span><span class="p">,</span> <span class="n">scale</span><span class="p">):</span>
        <span class="o">...</span>
</pre></div>
</div>
<p>In the <tt class="xref py py-class docutils literal"><span class="pre">Drawable</span></tt> ABC above, the <tt class="xref py py-meth docutils literal"><span class="pre">draw_doubled()</span></tt> method
renders the object at twice its size and can be implemented in terms
of other methods described in <tt class="xref py py-class docutils literal"><span class="pre">Drawable</span></tt>.  Classes implementing
this ABC therefore don&#8217;t need to provide their own implementation
of <tt class="xref py py-meth docutils literal"><span class="pre">draw_doubled()</span></tt>, though they can do so.  An implementation
of <tt class="xref py py-meth docutils literal"><span class="pre">draw()</span></tt> is necessary, though; the ABC can&#8217;t provide
a useful generic implementation.</p>
<p>You can apply the <tt class="docutils literal"><span class="pre">&#64;abstractmethod</span></tt> decorator to methods such as
<tt class="xref py py-meth docutils literal"><span class="pre">draw()</span></tt> that must be implemented; Python will then raise an
exception for classes that don&#8217;t define the method.
Note that the exception is only raised when you actually
try to create an instance of a subclass lacking the method:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">class</span> <span class="nc">Circle</span><span class="p">(</span><span class="n">Drawable</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">pass</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">c</span> <span class="o">=</span> <span class="n">Circle</span><span class="p">()</span>
<span class="gt">Traceback (most recent call last):</span>
  File <span class="nb">&quot;&lt;stdin&gt;&quot;</span>, line <span class="m">1</span>, in <span class="n-Identifier">&lt;module&gt;</span>
<span class="nc">TypeError</span>: <span class="n-Identifier">Can&#39;t instantiate abstract class Circle with abstract methods draw</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>Abstract data attributes can be declared using the
<tt class="docutils literal"><span class="pre">&#64;abstractproperty</span></tt> decorator:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">abc</span> <span class="kn">import</span> <span class="n">abstractproperty</span>
<span class="o">...</span>

<span class="nd">@abstractproperty</span>
<span class="k">def</span> <span class="nf">readonly</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
   <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span>
</pre></div>
</div>
<p>Subclasses must then define a <tt class="xref py py-meth docutils literal"><span class="pre">readonly()</span></tt> property.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><span class="target" id="index-16"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3119"><strong>PEP 3119</strong></a> - Introducing Abstract Base Classes</dt>
<dd>PEP written by Guido van Rossum and Talin.
Implemented by Guido van Rossum.
Backported to 2.6 by Benjamin Aranguren, with Alex Martelli.</dd>
</dl>
</div>
</div>
<div class="section" id="pep-3127-integer-literal-support-and-syntax">
<span id="pep-3127"></span><h2>PEP 3127: Integer Literal Support and Syntax<a class="headerlink" href="#pep-3127-integer-literal-support-and-syntax" title="Permalink to this headline">¶</a></h2>
<p>Python 3.0 changes the syntax for octal (base-8) integer literals,
prefixing them with &#8220;0o&#8221; or &#8220;0O&#8221; instead of a leading zero, and adds
support for binary (base-2) integer literals, signalled by a &#8220;0b&#8221; or
&#8220;0B&#8221; prefix.</p>
<p>Python 2.6 doesn&#8217;t drop support for a leading 0 signalling
an octal number, but it does add support for &#8220;0o&#8221; and &#8220;0b&#8221;:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="mi">0</span><span class="n">o21</span><span class="p">,</span> <span class="mi">2</span><span class="o">*</span><span class="mi">8</span> <span class="o">+</span> <span class="mi">1</span>
<span class="go">(17, 17)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="mi">0</span><span class="n">b101111</span>
<span class="go">47</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="../library/functions.html#oct" title="oct"><tt class="xref py py-func docutils literal"><span class="pre">oct()</span></tt></a> builtin still returns numbers
prefixed with a leading zero, and a new <a class="reference internal" href="../library/functions.html#bin" title="bin"><tt class="xref py py-func docutils literal"><span class="pre">bin()</span></tt></a>
builtin returns the binary representation for a number:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">oct</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="go">&#39;052&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">future_builtins</span><span class="o">.</span><span class="n">oct</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span>
<span class="go">&#39;0o52&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">bin</span><span class="p">(</span><span class="mi">173</span><span class="p">)</span>
<span class="go">&#39;0b10101101&#39;</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="../library/functions.html#int" title="int"><tt class="xref py py-func docutils literal"><span class="pre">int()</span></tt></a> and <a class="reference internal" href="../library/functions.html#long" title="long"><tt class="xref py py-func docutils literal"><span class="pre">long()</span></tt></a> builtins will now accept the &#8220;0o&#8221;
and &#8220;0b&#8221; prefixes when base-8 or base-2 are requested, or when the
<em>base</em> argument is zero (signalling that the base used should be
determined from the string):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">int</span> <span class="p">(</span><span class="s">&#39;0o52&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="go">42</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">int</span><span class="p">(</span><span class="s">&#39;1101&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">13</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">int</span><span class="p">(</span><span class="s">&#39;0b1101&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="go">13</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">int</span><span class="p">(</span><span class="s">&#39;0b1101&#39;</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span>
<span class="go">13</span>
</pre></div>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><span class="target" id="index-17"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3127"><strong>PEP 3127</strong></a> - Integer Literal Support and Syntax</dt>
<dd>PEP written by Patrick Maupin; backported to 2.6 by
Eric Smith.</dd>
</dl>
</div>
</div>
<div class="section" id="pep-3129-class-decorators">
<span id="pep-3129"></span><h2>PEP 3129: Class Decorators<a class="headerlink" href="#pep-3129-class-decorators" title="Permalink to this headline">¶</a></h2>
<p>Decorators have been extended from functions to classes.  It&#8217;s now legal to
write:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="nd">@foo</span>
<span class="nd">@bar</span>
<span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
  <span class="k">pass</span>
</pre></div>
</div>
<p>This is equivalent to:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">A</span><span class="p">:</span>
  <span class="k">pass</span>

<span class="n">A</span> <span class="o">=</span> <span class="n">foo</span><span class="p">(</span><span class="n">bar</span><span class="p">(</span><span class="n">A</span><span class="p">))</span>
</pre></div>
</div>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt><span class="target" id="index-18"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3129"><strong>PEP 3129</strong></a> - Class Decorators</dt>
<dd>PEP written by Collin Winter.</dd>
</dl>
</div>
</div>
<div class="section" id="pep-3141-a-type-hierarchy-for-numbers">
<span id="pep-3141"></span><h2>PEP 3141: A Type Hierarchy for Numbers<a class="headerlink" href="#pep-3141-a-type-hierarchy-for-numbers" title="Permalink to this headline">¶</a></h2>
<p>Python 3.0 adds several abstract base classes for numeric types
inspired by Scheme&#8217;s numeric tower.  These classes were backported to
2.6 as the <a class="reference internal" href="../library/numbers.html#module-numbers" title="numbers: Numeric abstract base classes (Complex, Real, Integral, etc.)."><tt class="xref py py-mod docutils literal"><span class="pre">numbers</span></tt></a> module.</p>
<p>The most general ABC is <tt class="xref py py-class docutils literal"><span class="pre">Number</span></tt>.  It defines no operations at
all, and only exists to allow checking if an object is a number by
doing <tt class="docutils literal"><span class="pre">isinstance(obj,</span> <span class="pre">Number)</span></tt>.</p>
<p><tt class="xref py py-class docutils literal"><span class="pre">Complex</span></tt> is a subclass of <tt class="xref py py-class docutils literal"><span class="pre">Number</span></tt>.  Complex numbers
can undergo the basic operations of addition, subtraction,
multiplication, division, and exponentiation, and you can retrieve the
real and imaginary parts and obtain a number&#8217;s conjugate.  Python&#8217;s built-in
complex type is an implementation of <tt class="xref py py-class docutils literal"><span class="pre">Complex</span></tt>.</p>
<p><tt class="xref py py-class docutils literal"><span class="pre">Real</span></tt> further derives from <tt class="xref py py-class docutils literal"><span class="pre">Complex</span></tt>, and adds
operations that only work on real numbers: <tt class="xref py py-func docutils literal"><span class="pre">floor()</span></tt>, <tt class="xref py py-func docutils literal"><span class="pre">trunc()</span></tt>,
rounding, taking the remainder mod N, floor division,
and comparisons.</p>
<p><tt class="xref py py-class docutils literal"><span class="pre">Rational</span></tt> numbers derive from <tt class="xref py py-class docutils literal"><span class="pre">Real</span></tt>, have
<tt class="xref py py-attr docutils literal"><span class="pre">numerator</span></tt> and <tt class="xref py py-attr docutils literal"><span class="pre">denominator</span></tt> properties, and can be
converted to floats.  Python 2.6 adds a simple rational-number class,
<tt class="xref py py-class docutils literal"><span class="pre">Fraction</span></tt>, in the <a class="reference internal" href="../library/fractions.html#module-fractions" title="fractions: Rational numbers."><tt class="xref py py-mod docutils literal"><span class="pre">fractions</span></tt></a> module.  (It&#8217;s called
<tt class="xref py py-class docutils literal"><span class="pre">Fraction</span></tt> instead of <tt class="xref py py-class docutils literal"><span class="pre">Rational</span></tt> to avoid
a name clash with <a class="reference internal" href="../library/numbers.html#numbers.Rational" title="numbers.Rational"><tt class="xref py py-class docutils literal"><span class="pre">numbers.Rational</span></tt></a>.)</p>
<p><tt class="xref py py-class docutils literal"><span class="pre">Integral</span></tt> numbers derive from <tt class="xref py py-class docutils literal"><span class="pre">Rational</span></tt>, and
can be shifted left and right with <tt class="docutils literal"><span class="pre">&lt;&lt;</span></tt> and <tt class="docutils literal"><span class="pre">&gt;&gt;</span></tt>,
combined using bitwise operations such as <tt class="docutils literal"><span class="pre">&amp;</span></tt> and <tt class="docutils literal"><span class="pre">|</span></tt>,
and can be used as array indexes and slice boundaries.</p>
<p>In Python 3.0, the PEP slightly redefines the existing builtins
<a class="reference internal" href="../library/functions.html#round" title="round"><tt class="xref py py-func docutils literal"><span class="pre">round()</span></tt></a>, <a class="reference internal" href="../library/math.html#math.floor" title="math.floor"><tt class="xref py py-func docutils literal"><span class="pre">math.floor()</span></tt></a>, <a class="reference internal" href="../library/math.html#math.ceil" title="math.ceil"><tt class="xref py py-func docutils literal"><span class="pre">math.ceil()</span></tt></a>, and adds a new
one, <a class="reference internal" href="../library/math.html#math.trunc" title="math.trunc"><tt class="xref py py-func docutils literal"><span class="pre">math.trunc()</span></tt></a>, that&#8217;s been backported to Python 2.6.
<a class="reference internal" href="../library/math.html#math.trunc" title="math.trunc"><tt class="xref py py-func docutils literal"><span class="pre">math.trunc()</span></tt></a> rounds toward zero, returning the closest
<tt class="xref py py-class docutils literal"><span class="pre">Integral</span></tt> that&#8217;s between the function&#8217;s argument and zero.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="docutils">
<dt><span class="target" id="index-19"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-3141"><strong>PEP 3141</strong></a> - A Type Hierarchy for Numbers</dt>
<dd>PEP written by Jeffrey Yasskin.</dd>
</dl>
<p><a class="reference external" href="http://www.gnu.org/software/guile/manual/html_node/Numerical-Tower.html#Numerical-Tower">Scheme&#8217;s numerical tower</a>, from the Guile manual.</p>
<p class="last"><a class="reference external" href="http://schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html#%_sec_6.2">Scheme&#8217;s number datatypes</a> from the R5RS Scheme specification.</p>
</div>
<div class="section" id="the-fractions-module">
<h3>The <a class="reference internal" href="../library/fractions.html#module-fractions" title="fractions: Rational numbers."><tt class="xref py py-mod docutils literal"><span class="pre">fractions</span></tt></a> Module<a class="headerlink" href="#the-fractions-module" title="Permalink to this headline">¶</a></h3>
<p>To fill out the hierarchy of numeric types, the <a class="reference internal" href="../library/fractions.html#module-fractions" title="fractions: Rational numbers."><tt class="xref py py-mod docutils literal"><span class="pre">fractions</span></tt></a>
module provides a rational-number class.  Rational numbers store their
values as a numerator and denominator forming a fraction, and can
exactly represent numbers such as <tt class="docutils literal"><span class="pre">2/3</span></tt> that floating-point numbers
can only approximate.</p>
<p>The <tt class="xref py py-class docutils literal"><span class="pre">Fraction</span></tt> constructor takes two <tt class="xref py py-class docutils literal"><span class="pre">Integral</span></tt> values
that will be the numerator and denominator of the resulting fraction.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">fractions</span> <span class="kn">import</span> <span class="n">Fraction</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="n">Fraction</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span> <span class="o">=</span> <span class="n">Fraction</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="p">(</span><span class="n">a</span><span class="p">),</span> <span class="nb">float</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
<span class="go">(0.66666666666666663, 0.40000000000000002)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">+</span><span class="n">b</span>
<span class="go">Fraction(16, 15)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">/</span><span class="n">b</span>
<span class="go">Fraction(5, 3)</span>
</pre></div>
</div>
<p>For converting floating-point numbers to rationals,
the float type now has an <tt class="xref py py-meth docutils literal"><span class="pre">as_integer_ratio()</span></tt> method that returns
the numerator and denominator for a fraction that evaluates to the same
floating-point value:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="mf">2.5</span><span class="p">)</span> <span class="o">.</span><span class="n">as_integer_ratio</span><span class="p">()</span>
<span class="go">(5, 2)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="mf">3.1415</span><span class="p">)</span> <span class="o">.</span><span class="n">as_integer_ratio</span><span class="p">()</span>
<span class="go">(7074029114692207L, 2251799813685248L)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="p">(</span><span class="mf">1.</span><span class="o">/</span><span class="mi">3</span><span class="p">)</span> <span class="o">.</span><span class="n">as_integer_ratio</span><span class="p">()</span>
<span class="go">(6004799503160661L, 18014398509481984L)</span>
</pre></div>
</div>
<p>Note that values that can only be approximated by floating-point
numbers, such as 1./3, are not simplified to the number being
approximated; the fraction attempts to match the floating-point value
<strong>exactly</strong>.</p>
<p>The <a class="reference internal" href="../library/fractions.html#module-fractions" title="fractions: Rational numbers."><tt class="xref py py-mod docutils literal"><span class="pre">fractions</span></tt></a> module is based upon an implementation by Sjoerd
Mullender that was in Python&#8217;s <tt class="file docutils literal"><span class="pre">Demo/classes/</span></tt> directory for a
long time.  This implementation was significantly updated by Jeffrey
Yasskin.</p>
</div>
</div>
<div class="section" id="other-language-changes">
<h2>Other Language Changes<a class="headerlink" href="#other-language-changes" title="Permalink to this headline">¶</a></h2>
<p>Some smaller changes made to the core Python language are:</p>
<ul>
<li><p class="first">Directories and zip archives containing a <tt class="file docutils literal"><span class="pre">__main__.py</span></tt> file
can now be executed directly by passing their name to the
interpreter. The directory or zip archive is automatically inserted
as the first entry in sys.path.  (Suggestion and initial patch by
Andy Chu, subsequently revised by Phillip J. Eby and Nick Coghlan;
<a class="reference external" href="http://bugs.python.org/issue1739468">issue 1739468</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/functions.html#hasattr" title="hasattr"><tt class="xref py py-func docutils literal"><span class="pre">hasattr()</span></tt></a> function was catching and ignoring all errors,
under the assumption that they meant a <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattr__()</span></tt></a> method
was failing somehow and the return value of <a class="reference internal" href="../library/functions.html#hasattr" title="hasattr"><tt class="xref py py-func docutils literal"><span class="pre">hasattr()</span></tt></a> would
therefore be <tt class="xref docutils literal"><span class="pre">False</span></tt>.  This logic shouldn&#8217;t be applied to
<a class="reference internal" href="../library/exceptions.html#exceptions.KeyboardInterrupt" title="exceptions.KeyboardInterrupt"><tt class="xref py py-exc docutils literal"><span class="pre">KeyboardInterrupt</span></tt></a> and <a class="reference internal" href="../library/exceptions.html#exceptions.SystemExit" title="exceptions.SystemExit"><tt class="xref py py-exc docutils literal"><span class="pre">SystemExit</span></tt></a>, however; Python 2.6
will no longer discard such exceptions when <a class="reference internal" href="../library/functions.html#hasattr" title="hasattr"><tt class="xref py py-func docutils literal"><span class="pre">hasattr()</span></tt></a>
encounters them.  (Fixed by Benjamin Peterson; <a class="reference external" href="http://bugs.python.org/issue2196">issue 2196</a>.)</p>
</li>
<li><p class="first">When calling a function using the <tt class="docutils literal"><span class="pre">**</span></tt> syntax to provide keyword
arguments, you are no longer required to use a Python dictionary;
any mapping will now work:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="o">**</span><span class="n">kw</span><span class="p">):</span>
<span class="gp">... </span>   <span class="k">print</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">kw</span><span class="p">)</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ud</span><span class="o">=</span><span class="n">UserDict</span><span class="o">.</span><span class="n">UserDict</span><span class="p">()</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ud</span><span class="p">[</span><span class="s">&#39;a&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="mi">1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">ud</span><span class="p">[</span><span class="s">&#39;b&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s">&#39;string&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="o">**</span><span class="n">ud</span><span class="p">)</span>
<span class="go">[&#39;a&#39;, &#39;b&#39;]</span>
</pre></div>
</div>
<p>(Contributed by Alexander Belopolsky; <a class="reference external" href="http://bugs.python.org/issue1686487">issue 1686487</a>.)</p>
<p>It&#8217;s also become legal to provide keyword arguments after a <tt class="docutils literal"><span class="pre">*args</span></tt> argument
to a function call.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="k">def</span> <span class="nf">f</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kw</span><span class="p">):</span>
<span class="gp">... </span>    <span class="k">print</span> <span class="n">args</span><span class="p">,</span> <span class="n">kw</span>
<span class="gp">...</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">f</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span> <span class="o">*</span><span class="p">(</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">),</span> <span class="n">keyword</span><span class="o">=</span><span class="mi">13</span><span class="p">)</span>
<span class="go">(1, 2, 3, 4, 5, 6) {&#39;keyword&#39;: 13}</span>
</pre></div>
</div>
<p>Previously this would have been a syntax error.
(Contributed by Amaury Forgeot d&#8217;Arc; <a class="reference external" href="http://bugs.python.org/issue3473">issue 3473</a>.)</p>
</li>
<li><p class="first">A new builtin, <tt class="docutils literal"><span class="pre">next(iterator,</span> <span class="pre">[default])</span></tt> returns the next item
from the specified iterator.  If the <em>default</em> argument is supplied,
it will be returned if <em>iterator</em> has been exhausted; otherwise,
the <a class="reference internal" href="../library/exceptions.html#exceptions.StopIteration" title="exceptions.StopIteration"><tt class="xref py py-exc docutils literal"><span class="pre">StopIteration</span></tt></a> exception will be raised.  (Backported
in <a class="reference external" href="http://bugs.python.org/issue2719">issue 2719</a>.)</p>
</li>
<li><p class="first">Tuples now have <tt class="xref py py-meth docutils literal"><span class="pre">index()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">count()</span></tt> methods matching the
list type&#8217;s <tt class="xref py py-meth docutils literal"><span class="pre">index()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">count()</span></tt> methods:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">t</span> <span class="o">=</span> <span class="p">(</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">0</span><span class="p">,</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="go">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">t</span><span class="o">.</span><span class="n">count</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
<span class="go">2</span>
</pre></div>
</div>
<p>(Contributed by Raymond Hettinger)</p>
</li>
<li><p class="first">The built-in types now have improved support for extended slicing syntax,
accepting various combinations of <tt class="docutils literal"><span class="pre">(start,</span> <span class="pre">stop,</span> <span class="pre">step)</span></tt>.
Previously, the support was partial and certain corner cases wouldn&#8217;t work.
(Implemented by Thomas Wouters.)</p>
</li>
<li><p class="first">Properties now have three attributes, <tt class="xref py py-attr docutils literal"><span class="pre">getter</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">setter</span></tt>
and <tt class="xref py py-attr docutils literal"><span class="pre">deleter</span></tt>, that are decorators providing useful shortcuts
for adding a getter, setter or deleter function to an existing
property. You would use them like this:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">class</span> <span class="nc">C</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="nd">@property</span>
    <span class="k">def</span> <span class="nf">x</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span>

    <span class="nd">@x.setter</span>
    <span class="k">def</span> <span class="nf">x</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_x</span> <span class="o">=</span> <span class="n">value</span>

    <span class="nd">@x.deleter</span>
    <span class="k">def</span> <span class="nf">x</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span>

<span class="k">class</span> <span class="nc">D</span><span class="p">(</span><span class="n">C</span><span class="p">):</span>
    <span class="nd">@C.x.getter</span>
    <span class="k">def</span> <span class="nf">x</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_x</span> <span class="o">*</span> <span class="mi">2</span>

    <span class="nd">@x.setter</span>
    <span class="k">def</span> <span class="nf">x</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_x</span> <span class="o">=</span> <span class="n">value</span> <span class="o">/</span> <span class="mi">2</span>
</pre></div>
</div>
</li>
<li><p class="first">Several methods of the built-in set types now accept multiple iterables:
<tt class="xref py py-meth docutils literal"><span class="pre">intersection()</span></tt>,
<tt class="xref py py-meth docutils literal"><span class="pre">intersection_update()</span></tt>,
<tt class="xref py py-meth docutils literal"><span class="pre">union()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">update()</span></tt>,
<tt class="xref py py-meth docutils literal"><span class="pre">difference()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">difference_update()</span></tt>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="o">=</span><span class="nb">set</span><span class="p">(</span><span class="s">&#39;1234567890&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="o">.</span><span class="n">intersection</span><span class="p">(</span><span class="s">&#39;abc123&#39;</span><span class="p">,</span> <span class="s">&#39;cdf246&#39;</span><span class="p">)</span>  <span class="c"># Intersection between all inputs</span>
<span class="go">set([&#39;2&#39;])</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">s</span><span class="o">.</span><span class="n">difference</span><span class="p">(</span><span class="s">&#39;246&#39;</span><span class="p">,</span> <span class="s">&#39;789&#39;</span><span class="p">)</span>
<span class="go">set([&#39;1&#39;, &#39;0&#39;, &#39;3&#39;, &#39;5&#39;])</span>
</pre></div>
</div>
<p>(Contributed by Raymond Hettinger.)</p>
</li>
<li><p class="first">Many floating-point features were added.  The <a class="reference internal" href="../library/functions.html#float" title="float"><tt class="xref py py-func docutils literal"><span class="pre">float()</span></tt></a> function
will now turn the string <tt class="docutils literal"><span class="pre">nan</span></tt> into an
IEEE 754 Not A Number value, and <tt class="docutils literal"><span class="pre">+inf</span></tt> and <tt class="docutils literal"><span class="pre">-inf</span></tt> into
positive or negative infinity.  This works on any platform with
IEEE 754 semantics.  (Contributed by Christian Heimes; <a class="reference external" href="http://bugs.python.org/issue1635">issue 1635</a>.)</p>
<p>Other functions in the <a class="reference internal" href="../library/math.html#module-math" title="math: Mathematical functions (sin() etc.)."><tt class="xref py py-mod docutils literal"><span class="pre">math</span></tt></a> module, <tt class="xref py py-func docutils literal"><span class="pre">isinf()</span></tt> and
<tt class="xref py py-func docutils literal"><span class="pre">isnan()</span></tt>, return true if their floating-point argument is
infinite or Not A Number.  (<a class="reference external" href="http://bugs.python.org/issue1640">issue 1640</a>)</p>
<p>Conversion functions were added to convert floating-point numbers
into hexadecimal strings (<a class="reference external" href="http://bugs.python.org/issue3008">issue 3008</a>).  These functions
convert floats to and from a string representation without
introducing rounding errors from the conversion between decimal and
binary.  Floats have a <a class="reference internal" href="../library/functions.html#hex" title="hex"><tt class="xref py py-meth docutils literal"><span class="pre">hex()</span></tt></a> method that returns a string
representation, and the <tt class="docutils literal"><span class="pre">float.fromhex()</span></tt> method converts a string
back into a number:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">a</span> <span class="o">=</span> <span class="mf">3.75</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">a</span><span class="o">.</span><span class="n">hex</span><span class="p">()</span>
<span class="go">&#39;0x1.e000000000000p+1&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">float</span><span class="o">.</span><span class="n">fromhex</span><span class="p">(</span><span class="s">&#39;0x1.e000000000000p+1&#39;</span><span class="p">)</span>
<span class="go">3.75</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">=</span><span class="mf">1.</span><span class="o">/</span><span class="mi">3</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">b</span><span class="o">.</span><span class="n">hex</span><span class="p">()</span>
<span class="go">&#39;0x1.5555555555555p-2&#39;</span>
</pre></div>
</div>
</li>
<li><p class="first">A numerical nicety: when creating a complex number from two floats
on systems that support signed zeros (-0 and +0), the
<a class="reference internal" href="../library/functions.html#complex" title="complex"><tt class="xref py py-func docutils literal"><span class="pre">complex()</span></tt></a> constructor will now preserve the sign
of the zero.  (Fixed by Mark T. Dickinson; <a class="reference external" href="http://bugs.python.org/issue1507">issue 1507</a>.)</p>
</li>
<li><p class="first">Classes that inherit a <a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><tt class="xref py py-meth docutils literal"><span class="pre">__hash__()</span></tt></a> method from a parent class
can set <tt class="docutils literal"><span class="pre">__hash__</span> <span class="pre">=</span> <span class="pre">None</span></tt> to indicate that the class isn&#8217;t
hashable.  This will make <tt class="docutils literal"><span class="pre">hash(obj)</span></tt> raise a <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a>
and the class will not be indicated as implementing the
<tt class="xref py py-class docutils literal"><span class="pre">Hashable</span></tt> ABC.</p>
<p>You should do this when you&#8217;ve defined a <a class="reference internal" href="../reference/datamodel.html#object.__cmp__" title="object.__cmp__"><tt class="xref py py-meth docutils literal"><span class="pre">__cmp__()</span></tt></a> or
<a class="reference internal" href="../reference/datamodel.html#object.__eq__" title="object.__eq__"><tt class="xref py py-meth docutils literal"><span class="pre">__eq__()</span></tt></a> method that compares objects by their value rather
than by identity.  All objects have a default hash method that uses
<tt class="docutils literal"><span class="pre">id(obj)</span></tt> as the hash value.  There&#8217;s no tidy way to remove the
<a class="reference internal" href="../reference/datamodel.html#object.__hash__" title="object.__hash__"><tt class="xref py py-meth docutils literal"><span class="pre">__hash__()</span></tt></a> method inherited from a parent class, so
assigning <tt class="xref docutils literal"><span class="pre">None</span></tt> was implemented as an override.  At the
C level, extensions can set <tt class="docutils literal"><span class="pre">tp_hash</span></tt> to
<a class="reference internal" href="../c-api/object.html#PyObject_HashNotImplemented" title="PyObject_HashNotImplemented"><tt class="xref c c-func docutils literal"><span class="pre">PyObject_HashNotImplemented()</span></tt></a>.
(Fixed by Nick Coghlan and Amaury Forgeot d&#8217;Arc; <a class="reference external" href="http://bugs.python.org/issue2235">issue 2235</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/exceptions.html#exceptions.GeneratorExit" title="exceptions.GeneratorExit"><tt class="xref py py-exc docutils literal"><span class="pre">GeneratorExit</span></tt></a> exception now subclasses
<a class="reference internal" href="../library/exceptions.html#exceptions.BaseException" title="exceptions.BaseException"><tt class="xref py py-exc docutils literal"><span class="pre">BaseException</span></tt></a> instead of <a class="reference internal" href="../library/exceptions.html#exceptions.Exception" title="exceptions.Exception"><tt class="xref py py-exc docutils literal"><span class="pre">Exception</span></tt></a>.  This means
that an exception handler that does <tt class="docutils literal"><span class="pre">except</span> <span class="pre">Exception:</span></tt>
will not inadvertently catch <a class="reference internal" href="../library/exceptions.html#exceptions.GeneratorExit" title="exceptions.GeneratorExit"><tt class="xref py py-exc docutils literal"><span class="pre">GeneratorExit</span></tt></a>.
(Contributed by Chad Austin; <a class="reference external" href="http://bugs.python.org/issue1537">issue 1537</a>.)</p>
</li>
<li><p class="first">Generator objects now have a <tt class="xref py py-attr docutils literal"><span class="pre">gi_code</span></tt> attribute that refers to
the original code object backing the generator.
(Contributed by Collin Winter; <a class="reference external" href="http://bugs.python.org/issue1473257">issue 1473257</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/functions.html#compile" title="compile"><tt class="xref py py-func docutils literal"><span class="pre">compile()</span></tt></a> built-in function now accepts keyword arguments
as well as positional parameters.  (Contributed by Thomas Wouters;
<a class="reference external" href="http://bugs.python.org/issue1444529">issue 1444529</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/functions.html#complex" title="complex"><tt class="xref py py-func docutils literal"><span class="pre">complex()</span></tt></a> constructor now accepts strings containing
parenthesized complex numbers, meaning that <tt class="docutils literal"><span class="pre">complex(repr(cplx))</span></tt>
will now round-trip values.  For example, <tt class="docutils literal"><span class="pre">complex('(3+4j)')</span></tt>
now returns the value (3+4j).  (<a class="reference external" href="http://bugs.python.org/issue1491866">issue 1491866</a>)</p>
</li>
<li><p class="first">The string <tt class="xref py py-meth docutils literal"><span class="pre">translate()</span></tt> method now accepts <tt class="xref docutils literal"><span class="pre">None</span></tt> as the
translation table parameter, which is treated as the identity
transformation.   This makes it easier to carry out operations
that only delete characters.  (Contributed by Bengt Richter and
implemented by Raymond Hettinger; <a class="reference external" href="http://bugs.python.org/issue1193128">issue 1193128</a>.)</p>
</li>
<li><p class="first">The built-in <a class="reference internal" href="../library/functions.html#dir" title="dir"><tt class="xref py py-func docutils literal"><span class="pre">dir()</span></tt></a> function now checks for a <tt class="xref py py-meth docutils literal"><span class="pre">__dir__()</span></tt>
method on the objects it receives.  This method must return a list
of strings containing the names of valid attributes for the object,
and lets the object control the value that <a class="reference internal" href="../library/functions.html#dir" title="dir"><tt class="xref py py-func docutils literal"><span class="pre">dir()</span></tt></a> produces.
Objects that have <a class="reference internal" href="../reference/datamodel.html#object.__getattr__" title="object.__getattr__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattr__()</span></tt></a> or <a class="reference internal" href="../reference/datamodel.html#object.__getattribute__" title="object.__getattribute__"><tt class="xref py py-meth docutils literal"><span class="pre">__getattribute__()</span></tt></a>
methods can use this to advertise pseudo-attributes they will honor.
(<a class="reference external" href="http://bugs.python.org/issue1591665">issue 1591665</a>)</p>
</li>
<li><p class="first">Instance method objects have new attributes for the object and function
comprising the method; the new synonym for <tt class="xref py py-attr docutils literal"><span class="pre">im_self</span></tt> is
<tt class="xref py py-attr docutils literal"><span class="pre">__self__</span></tt>, and <tt class="xref py py-attr docutils literal"><span class="pre">im_func</span></tt> is also available as <tt class="xref py py-attr docutils literal"><span class="pre">__func__</span></tt>.
The old names are still supported in Python 2.6, but are gone in 3.0.</p>
</li>
<li><p class="first">An obscure change: when you use the <a class="reference internal" href="../library/functions.html#locals" title="locals"><tt class="xref py py-func docutils literal"><span class="pre">locals()</span></tt></a> function inside a
<a class="reference internal" href="../reference/compound_stmts.html#class"><tt class="xref std std-keyword docutils literal"><span class="pre">class</span></tt></a> statement, the resulting dictionary no longer returns free
variables.  (Free variables, in this case, are variables referenced in the
<a class="reference internal" href="../reference/compound_stmts.html#class"><tt class="xref std std-keyword docutils literal"><span class="pre">class</span></tt></a> statement  that aren&#8217;t attributes of the class.)</p>
</li>
</ul>
<div class="section" id="optimizations">
<h3>Optimizations<a class="headerlink" href="#optimizations" title="Permalink to this headline">¶</a></h3>
<ul>
<li><p class="first">The <a class="reference internal" href="../library/warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><tt class="xref py py-mod docutils literal"><span class="pre">warnings</span></tt></a> module has been rewritten in C.  This makes
it possible to invoke warnings from the parser, and may also
make the interpreter&#8217;s startup faster.
(Contributed by Neal Norwitz and Brett Cannon; <a class="reference external" href="http://bugs.python.org/issue1631171">issue 1631171</a>.)</p>
</li>
<li><p class="first">Type objects now have a cache of methods that can reduce
the work required to find the correct method implementation
for a particular class; once cached, the interpreter doesn&#8217;t need to
traverse base classes to figure out the right method to call.
The cache is cleared if a base class or the class itself is modified,
so the cache should remain correct even in the face of Python&#8217;s dynamic
nature.
(Original optimization implemented by Armin Rigo, updated for
Python 2.6 by Kevin Jacobs; <a class="reference external" href="http://bugs.python.org/issue1700288">issue 1700288</a>.)</p>
<p>By default, this change is only applied to types that are included with
the Python core.  Extension modules may not necessarily be compatible with
this cache,
so they must explicitly add <tt class="xref c c-macro docutils literal"><span class="pre">Py_TPFLAGS_HAVE_VERSION_TAG</span></tt>
to the module&#8217;s <tt class="docutils literal"><span class="pre">tp_flags</span></tt> field to enable the method cache.
(To be compatible with the method cache, the extension module&#8217;s code
must not directly access and modify the <tt class="docutils literal"><span class="pre">tp_dict</span></tt> member of
any of the types it implements.  Most modules don&#8217;t do this,
but it&#8217;s impossible for the Python interpreter to determine that.
See <a class="reference external" href="http://bugs.python.org/issue1878">issue 1878</a> for some discussion.)</p>
</li>
<li><p class="first">Function calls that use keyword arguments are significantly faster
by doing a quick pointer comparison, usually saving the time of a
full string comparison.  (Contributed by Raymond Hettinger, after an
initial implementation by Antoine Pitrou; <a class="reference external" href="http://bugs.python.org/issue1819">issue 1819</a>.)</p>
</li>
<li><p class="first">All of the functions in the <a class="reference internal" href="../library/struct.html#module-struct" title="struct: Interpret strings as packed binary data."><tt class="xref py py-mod docutils literal"><span class="pre">struct</span></tt></a> module have been rewritten in
C, thanks to work at the Need For Speed sprint.
(Contributed by Raymond Hettinger.)</p>
</li>
<li><p class="first">Some of the standard built-in types now set a bit in their type
objects.  This speeds up checking whether an object is a subclass of
one of these types.  (Contributed by Neal Norwitz.)</p>
</li>
<li><p class="first">Unicode strings now use faster code for detecting
whitespace and line breaks; this speeds up the <tt class="xref py py-meth docutils literal"><span class="pre">split()</span></tt> method
by about 25% and <tt class="xref py py-meth docutils literal"><span class="pre">splitlines()</span></tt> by 35%.
(Contributed by Antoine Pitrou.)  Memory usage is reduced
by using pymalloc for the Unicode string&#8217;s data.</p>
</li>
<li><p class="first">The <tt class="docutils literal"><span class="pre">with</span></tt> statement now stores the <a class="reference internal" href="../reference/datamodel.html#object.__exit__" title="object.__exit__"><tt class="xref py py-meth docutils literal"><span class="pre">__exit__()</span></tt></a> method on the stack,
producing a small speedup.  (Implemented by Jeffrey Yasskin.)</p>
</li>
<li><p class="first">To reduce memory usage, the garbage collector will now clear internal
free lists when garbage-collecting the highest generation of objects.
This may return memory to the operating system sooner.</p>
</li>
</ul>
</div>
<div class="section" id="interpreter-changes">
<span id="new-26-interpreter"></span><h3>Interpreter Changes<a class="headerlink" href="#interpreter-changes" title="Permalink to this headline">¶</a></h3>
<p>Two command-line options have been reserved for use by other Python
implementations.  The <a class="reference internal" href="../using/cmdline.html#cmdoption-J"><em class="xref std std-option">-J</em></a> switch has been reserved for use by
Jython for Jython-specific options, such as switches that are passed to
the underlying JVM.  <a class="reference internal" href="../using/cmdline.html#cmdoption-X"><em class="xref std std-option">-X</em></a> has been reserved for options
specific to a particular implementation of Python such as CPython,
Jython, or IronPython.  If either option is used with Python 2.6, the
interpreter will report that the option isn&#8217;t currently used.</p>
<p>Python can now be prevented from writing <tt class="file docutils literal"><span class="pre">.pyc</span></tt> or <tt class="file docutils literal"><span class="pre">.pyo</span></tt>
files by supplying the <a class="reference internal" href="../using/cmdline.html#cmdoption-B"><em class="xref std std-option">-B</em></a> switch to the Python interpreter,
or by setting the <span class="target" id="index-20"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE"><tt class="xref std std-envvar docutils literal"><span class="pre">PYTHONDONTWRITEBYTECODE</span></tt></a> environment
variable before running the interpreter.  This setting is available to
Python programs as the <tt class="docutils literal"><span class="pre">sys.dont_write_bytecode</span></tt> variable, and
Python code can change the value to modify the interpreter&#8217;s
behaviour.  (Contributed by Neal Norwitz and Georg Brandl.)</p>
<p>The encoding used for standard input, output, and standard error can
be specified by setting the <span class="target" id="index-21"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONIOENCODING"><tt class="xref std std-envvar docutils literal"><span class="pre">PYTHONIOENCODING</span></tt></a> environment
variable before running the interpreter.  The value should be a string
in the form <tt class="docutils literal"><span class="pre">&lt;encoding&gt;</span></tt> or <tt class="docutils literal"><span class="pre">&lt;encoding&gt;:&lt;errorhandler&gt;</span></tt>.
The <em>encoding</em> part specifies the encoding&#8217;s name, e.g. <tt class="docutils literal"><span class="pre">utf-8</span></tt> or
<tt class="docutils literal"><span class="pre">latin-1</span></tt>; the optional <em>errorhandler</em> part specifies
what to do with characters that can&#8217;t be handled by the encoding,
and  should be one of &#8220;error&#8221;, &#8220;ignore&#8221;, or &#8220;replace&#8221;.   (Contributed
by Martin von Loewis.)</p>
</div>
</div>
<div class="section" id="new-and-improved-modules">
<h2>New and Improved Modules<a class="headerlink" href="#new-and-improved-modules" title="Permalink to this headline">¶</a></h2>
<p>As in every release, Python&#8217;s standard library received a number of
enhancements and bug fixes.  Here&#8217;s a partial list of the most notable
changes, sorted alphabetically by module name. Consult the
<tt class="file docutils literal"><span class="pre">Misc/NEWS</span></tt> file in the source tree for a more complete list of
changes, or look through the Subversion logs for all the details.</p>
<ul>
<li><p class="first">The <a class="reference internal" href="../library/asyncore.html#module-asyncore" title="asyncore: A base class for developing asynchronous socket handling services."><tt class="xref py py-mod docutils literal"><span class="pre">asyncore</span></tt></a> and <a class="reference internal" href="../library/asynchat.html#module-asynchat" title="asynchat: Support for asynchronous command/response protocols."><tt class="xref py py-mod docutils literal"><span class="pre">asynchat</span></tt></a> modules are
being actively maintained again, and a number of patches and bugfixes
were applied.  (Maintained by Josiah Carlson; see <a class="reference external" href="http://bugs.python.org/issue1736190">issue 1736190</a> for
one patch.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/bsddb.html#module-bsddb" title="bsddb: Interface to Berkeley DB database library"><tt class="xref py py-mod docutils literal"><span class="pre">bsddb</span></tt></a> module also has a new maintainer, Jesús Cea Avion, and the package
is now available as a standalone package.  The web page for the package is
<a class="reference external" href="http://www.jcea.es/programacion/pybsddb.htm">www.jcea.es/programacion/pybsddb.htm</a>.
The plan is to remove the package from the standard library
in Python 3.0, because its pace of releases is much more frequent than
Python&#8217;s.</p>
<p>The <tt class="xref py py-mod docutils literal"><span class="pre">bsddb.dbshelve</span></tt> module now uses the highest pickling protocol
available, instead of restricting itself to protocol 1.
(Contributed by W. Barnes.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/cgi.html#module-cgi" title="cgi: Helpers for running Python scripts via the Common Gateway Interface."><tt class="xref py py-mod docutils literal"><span class="pre">cgi</span></tt></a> module will now read variables from the query string
of an HTTP POST request.  This makes it possible to use form actions
with URLs that include query strings such as
&#8220;/cgi-bin/add.py?category=1&#8221;.  (Contributed by Alexandre Fiori and
Nubis; <a class="reference external" href="http://bugs.python.org/issue1817">issue 1817</a>.)</p>
<p>The <tt class="xref py py-func docutils literal"><span class="pre">parse_qs()</span></tt> and <tt class="xref py py-func docutils literal"><span class="pre">parse_qsl()</span></tt> functions have been
relocated from the <a class="reference internal" href="../library/cgi.html#module-cgi" title="cgi: Helpers for running Python scripts via the Common Gateway Interface."><tt class="xref py py-mod docutils literal"><span class="pre">cgi</span></tt></a> module to the <a class="reference internal" href="../library/urlparse.html#module-urlparse" title="urlparse: Parse URLs into or assemble them from components."><tt class="xref py py-mod docutils literal"><span class="pre">urlparse</span></tt></a> module.
The versions still available in the <a class="reference internal" href="../library/cgi.html#module-cgi" title="cgi: Helpers for running Python scripts via the Common Gateway Interface."><tt class="xref py py-mod docutils literal"><span class="pre">cgi</span></tt></a> module will
trigger <a class="reference internal" href="../library/exceptions.html#exceptions.PendingDeprecationWarning" title="exceptions.PendingDeprecationWarning"><tt class="xref py py-exc docutils literal"><span class="pre">PendingDeprecationWarning</span></tt></a> messages in 2.6
(<a class="reference external" href="http://bugs.python.org/issue600362">issue 600362</a>).</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/cmath.html#module-cmath" title="cmath: Mathematical functions for complex numbers."><tt class="xref py py-mod docutils literal"><span class="pre">cmath</span></tt></a> module underwent extensive revision,
contributed by Mark Dickinson and Christian Heimes.
Five new functions were added:</p>
<ul class="simple">
<li><tt class="xref py py-func docutils literal"><span class="pre">polar()</span></tt> converts a complex number to polar form, returning
the modulus and argument of the complex number.</li>
<li><tt class="xref py py-func docutils literal"><span class="pre">rect()</span></tt> does the opposite, turning a modulus, argument pair
back into the corresponding complex number.</li>
<li><tt class="xref py py-func docutils literal"><span class="pre">phase()</span></tt> returns the argument (also called the angle) of a complex
number.</li>
<li><tt class="xref py py-func docutils literal"><span class="pre">isnan()</span></tt> returns True if either
the real or imaginary part of its argument is a NaN.</li>
<li><tt class="xref py py-func docutils literal"><span class="pre">isinf()</span></tt> returns True if either the real or imaginary part of
its argument is infinite.</li>
</ul>
<p>The revisions also improved the numerical soundness of the
<a class="reference internal" href="../library/cmath.html#module-cmath" title="cmath: Mathematical functions for complex numbers."><tt class="xref py py-mod docutils literal"><span class="pre">cmath</span></tt></a> module.  For all functions, the real and imaginary
parts of the results are accurate to within a few units of least
precision (ulps) whenever possible.  See <a class="reference external" href="http://bugs.python.org/issue1381">issue 1381</a> for the
details.  The branch cuts for <tt class="xref py py-func docutils literal"><span class="pre">asinh()</span></tt>, <tt class="xref py py-func docutils literal"><span class="pre">atanh()</span></tt>: and
<tt class="xref py py-func docutils literal"><span class="pre">atan()</span></tt> have also been corrected.</p>
<p>The tests for the module have been greatly expanded; nearly 2000 new
test cases exercise the algebraic functions.</p>
<p>On IEEE 754 platforms, the <a class="reference internal" href="../library/cmath.html#module-cmath" title="cmath: Mathematical functions for complex numbers."><tt class="xref py py-mod docutils literal"><span class="pre">cmath</span></tt></a> module now handles IEEE 754
special values and floating-point exceptions in a manner consistent
with Annex &#8216;G&#8217; of the C99 standard.</p>
</li>
<li><p class="first">A new data type in the <a class="reference internal" href="../library/collections.html#module-collections" title="collections: High-performance datatypes"><tt class="xref py py-mod docutils literal"><span class="pre">collections</span></tt></a> module: <tt class="xref py py-class docutils literal"><span class="pre">namedtuple(typename,</span>
<span class="pre">fieldnames)</span></tt> is a factory function that creates subclasses of the standard tuple
whose fields are accessible by name as well as index.  For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">var_type</span> <span class="o">=</span> <span class="n">collections</span><span class="o">.</span><span class="n">namedtuple</span><span class="p">(</span><span class="s">&#39;variable&#39;</span><span class="p">,</span>
<span class="gp">... </span>            <span class="s">&#39;id name type size&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># Names are separated by spaces or commas.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="c"># &#39;id, name, type, size&#39; would also work.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">var_type</span><span class="o">.</span><span class="n">_fields</span>
<span class="go">(&#39;id&#39;, &#39;name&#39;, &#39;type&#39;, &#39;size&#39;)</span>

<span class="gp">&gt;&gt;&gt; </span><span class="n">var</span> <span class="o">=</span> <span class="n">var_type</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="s">&#39;frequency&#39;</span><span class="p">,</span> <span class="s">&#39;int&#39;</span><span class="p">,</span> <span class="mi">4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">var</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">var</span><span class="o">.</span><span class="n">id</span>    <span class="c"># Equivalent</span>
<span class="go">1 1</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">var</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="n">var</span><span class="o">.</span><span class="n">type</span>  <span class="c"># Equivalent</span>
<span class="go">int int</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">var</span><span class="o">.</span><span class="n">_asdict</span><span class="p">()</span>
<span class="go">{&#39;size&#39;: 4, &#39;type&#39;: &#39;int&#39;, &#39;id&#39;: 1, &#39;name&#39;: &#39;frequency&#39;}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v2</span> <span class="o">=</span> <span class="n">var</span><span class="o">.</span><span class="n">_replace</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s">&#39;amplitude&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">v2</span>
<span class="go">variable(id=1, name=&#39;amplitude&#39;, type=&#39;int&#39;, size=4)</span>
</pre></div>
</div>
<p>Several places in the standard library that returned tuples have
been modified to return <tt class="xref py py-class docutils literal"><span class="pre">namedtuple</span></tt> instances.  For example,
the <tt class="xref py py-meth docutils literal"><span class="pre">Decimal.as_tuple()</span></tt> method now returns a named tuple with
<tt class="xref py py-attr docutils literal"><span class="pre">sign</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">digits</span></tt>, and <tt class="xref py py-attr docutils literal"><span class="pre">exponent</span></tt> fields.</p>
<p>(Contributed by Raymond Hettinger.)</p>
</li>
<li><p class="first">Another change to the <a class="reference internal" href="../library/collections.html#module-collections" title="collections: High-performance datatypes"><tt class="xref py py-mod docutils literal"><span class="pre">collections</span></tt></a> module is that the
<tt class="xref py py-class docutils literal"><span class="pre">deque</span></tt> type now supports an optional <em>maxlen</em> parameter;
if supplied, the deque&#8217;s size will be restricted to no more
than <em>maxlen</em> items.  Adding more items to a full deque causes
old items to be discarded.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">from</span> <span class="nn">collections</span> <span class="kn">import</span> <span class="n">deque</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dq</span><span class="o">=</span><span class="n">deque</span><span class="p">(</span><span class="n">maxlen</span><span class="o">=</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dq</span>
<span class="go">deque([], maxlen=3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dq</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">1</span><span class="p">);</span> <span class="n">dq</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">2</span><span class="p">);</span> <span class="n">dq</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">3</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dq</span>
<span class="go">deque([1, 2, 3], maxlen=3)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dq</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">4</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">dq</span>
<span class="go">deque([2, 3, 4], maxlen=3)</span>
</pre></div>
</div>
<p>(Contributed by Raymond Hettinger.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/cookie.html#module-Cookie" title="Cookie: Support for HTTP state management (cookies)."><tt class="xref py py-mod docutils literal"><span class="pre">Cookie</span></tt></a> module&#8217;s <tt class="xref py py-class docutils literal"><span class="pre">Morsel</span></tt> objects now support an
<tt class="xref py py-attr docutils literal"><span class="pre">httponly</span></tt> attribute.  In some browsers. cookies with this attribute
set cannot be accessed or manipulated by JavaScript code.
(Contributed by Arvin Schnell; <a class="reference external" href="http://bugs.python.org/issue1638033">issue 1638033</a>.)</p>
</li>
<li><p class="first">A new window method in the <a class="reference internal" href="../library/curses.html#module-curses" title="curses: An interface to the curses library, providing portable terminal handling. (Unix)"><tt class="xref py py-mod docutils literal"><span class="pre">curses</span></tt></a> module,
<tt class="xref py py-meth docutils literal"><span class="pre">chgat()</span></tt>, changes the display attributes for a certain number of
characters on a single line.  (Contributed by Fabian Kreutz.)</p>
<div class="highlight-python"><div class="highlight"><pre><span class="c"># Boldface text starting at y=0,x=21</span>
<span class="c"># and affecting the rest of the line.</span>
<span class="n">stdscr</span><span class="o">.</span><span class="n">chgat</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">21</span><span class="p">,</span> <span class="n">curses</span><span class="o">.</span><span class="n">A_BOLD</span><span class="p">)</span>
</pre></div>
</div>
<p>The <tt class="xref py py-class docutils literal"><span class="pre">Textbox</span></tt> class in the <a class="reference internal" href="../library/curses.html#module-curses.textpad" title="curses.textpad: Emacs-like input editing in a curses window."><tt class="xref py py-mod docutils literal"><span class="pre">curses.textpad</span></tt></a> module
now supports editing in insert mode as well as overwrite mode.
Insert mode is enabled by supplying a true value for the <em>insert_mode</em>
parameter when creating the <tt class="xref py py-class docutils literal"><span class="pre">Textbox</span></tt> instance.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/datetime.html#module-datetime" title="datetime: Basic date and time types."><tt class="xref py py-mod docutils literal"><span class="pre">datetime</span></tt></a> module&#8217;s <tt class="xref py py-meth docutils literal"><span class="pre">strftime()</span></tt> methods now support a
<tt class="docutils literal"><span class="pre">%f</span></tt> format code that expands to the number of microseconds in the
object, zero-padded on
the left to six places.  (Contributed by Skip Montanaro; <a class="reference external" href="http://bugs.python.org/issue1158">issue 1158</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/decimal.html#module-decimal" title="decimal: Implementation of the General Decimal Arithmetic  Specification."><tt class="xref py py-mod docutils literal"><span class="pre">decimal</span></tt></a> module was updated to version 1.66 of
<a class="reference external" href="http://www2.hursley.ibm.com/decimal/decarith.html">the General Decimal Specification</a>.  New features
include some methods for some basic mathematical functions such as
<tt class="xref py py-meth docutils literal"><span class="pre">exp()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">log10()</span></tt>:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span><span class="o">.</span><span class="n">exp</span><span class="p">()</span>
<span class="go">Decimal(&quot;2.718281828459045235360287471&quot;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="s">&quot;2.7182818&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">ln</span><span class="p">()</span>
<span class="go">Decimal(&quot;0.9999999895305022877376682436&quot;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">Decimal</span><span class="p">(</span><span class="mi">1000</span><span class="p">)</span><span class="o">.</span><span class="n">log10</span><span class="p">()</span>
<span class="go">Decimal(&quot;3&quot;)</span>
</pre></div>
</div>
<p>The <tt class="xref py py-meth docutils literal"><span class="pre">as_tuple()</span></tt> method of <tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt> objects now returns a
named tuple with <tt class="xref py py-attr docutils literal"><span class="pre">sign</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">digits</span></tt>, and <tt class="xref py py-attr docutils literal"><span class="pre">exponent</span></tt> fields.</p>
<p>(Implemented by Facundo Batista and Mark Dickinson.  Named tuple
support added by Raymond Hettinger.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/difflib.html#module-difflib" title="difflib: Helpers for computing differences between objects."><tt class="xref py py-mod docutils literal"><span class="pre">difflib</span></tt></a> module&#8217;s <tt class="xref py py-class docutils literal"><span class="pre">SequenceMatcher</span></tt> class
now returns named tuples representing matches,
with <tt class="xref py py-attr docutils literal"><span class="pre">a</span></tt>, <tt class="xref py py-attr docutils literal"><span class="pre">b</span></tt>, and <tt class="xref py py-attr docutils literal"><span class="pre">size</span></tt> attributes.
(Contributed by Raymond Hettinger.)</p>
</li>
<li><p class="first">An optional <tt class="docutils literal"><span class="pre">timeout</span></tt> parameter, specifying a timeout measured in
seconds, was added to the <a class="reference internal" href="../library/ftplib.html#ftplib.FTP" title="ftplib.FTP"><tt class="xref py py-class docutils literal"><span class="pre">ftplib.FTP</span></tt></a> class constructor as
well as the <tt class="xref py py-meth docutils literal"><span class="pre">connect()</span></tt> method.  (Added by Facundo Batista.)
Also, the <tt class="xref py py-class docutils literal"><span class="pre">FTP</span></tt> class&#8217;s <tt class="xref py py-meth docutils literal"><span class="pre">storbinary()</span></tt> and
<tt class="xref py py-meth docutils literal"><span class="pre">storlines()</span></tt> now take an optional <em>callback</em> parameter that
will be called with each block of data after the data has been sent.
(Contributed by Phil Schwartz; <a class="reference external" href="http://bugs.python.org/issue1221598">issue 1221598</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/functions.html#reduce" title="reduce"><tt class="xref py py-func docutils literal"><span class="pre">reduce()</span></tt></a> built-in function is also available in the
<a class="reference internal" href="../library/functools.html#module-functools" title="functools: Higher-order functions and operations on callable objects."><tt class="xref py py-mod docutils literal"><span class="pre">functools</span></tt></a> module.  In Python 3.0, the builtin has been
dropped and <a class="reference internal" href="../library/functions.html#reduce" title="reduce"><tt class="xref py py-func docutils literal"><span class="pre">reduce()</span></tt></a> is only available from <a class="reference internal" href="../library/functools.html#module-functools" title="functools: Higher-order functions and operations on callable objects."><tt class="xref py py-mod docutils literal"><span class="pre">functools</span></tt></a>;
currently there are no plans to drop the builtin in the 2.x series.
(Patched by Christian Heimes; <a class="reference external" href="http://bugs.python.org/issue1739906">issue 1739906</a>.)</p>
</li>
<li><p class="first">When possible, the <a class="reference internal" href="../library/getpass.html#module-getpass" title="getpass: Portable reading of passwords and retrieval of the userid."><tt class="xref py py-mod docutils literal"><span class="pre">getpass</span></tt></a> module will now use
<tt class="file docutils literal"><span class="pre">/dev/tty</span></tt> to print a prompt message and read the password,
falling back to standard error and standard input.  If the
password may be echoed to the terminal, a warning is printed before
the prompt is displayed.  (Contributed by Gregory P. Smith.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/glob.html#glob.glob" title="glob.glob"><tt class="xref py py-func docutils literal"><span class="pre">glob.glob()</span></tt></a> function can now return Unicode filenames if
a Unicode path was used and Unicode filenames are matched within the
directory.  (<a class="reference external" href="http://bugs.python.org/issue1001604">issue 1001604</a>)</p>
</li>
<li><p class="first">A new function in the <a class="reference internal" href="../library/heapq.html#module-heapq" title="heapq: Heap queue algorithm (a.k.a. priority queue)."><tt class="xref py py-mod docutils literal"><span class="pre">heapq</span></tt></a> module, <tt class="docutils literal"><span class="pre">merge(iter1,</span> <span class="pre">iter2,</span> <span class="pre">...)</span></tt>,
takes any number of iterables returning data in sorted
order, and returns a new generator that returns the contents of all
the iterators, also in sorted order.  For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">heapq</span><span class="o">.</span><span class="n">merge</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">9</span><span class="p">],</span> <span class="p">[</span><span class="mi">2</span><span class="p">,</span> <span class="mi">8</span><span class="p">,</span> <span class="mi">16</span><span class="p">]))</span>
<span class="go">[1, 2, 3, 5, 8, 9, 16]</span>
</pre></div>
</div>
<p>Another new function, <tt class="docutils literal"><span class="pre">heappushpop(heap,</span> <span class="pre">item)</span></tt>,
pushes <em>item</em> onto <em>heap</em>, then pops off and returns the smallest item.
This is more efficient than making a call to <tt class="xref py py-func docutils literal"><span class="pre">heappush()</span></tt> and then
<tt class="xref py py-func docutils literal"><span class="pre">heappop()</span></tt>.</p>
<p><a class="reference internal" href="../library/heapq.html#module-heapq" title="heapq: Heap queue algorithm (a.k.a. priority queue)."><tt class="xref py py-mod docutils literal"><span class="pre">heapq</span></tt></a> is now implemented to only use less-than comparison,
instead of the less-than-or-equal comparison it previously used.
This makes <a class="reference internal" href="../library/heapq.html#module-heapq" title="heapq: Heap queue algorithm (a.k.a. priority queue)."><tt class="xref py py-mod docutils literal"><span class="pre">heapq</span></tt></a>&#8216;s usage of a type match the
<tt class="xref py py-meth docutils literal"><span class="pre">list.sort()</span></tt> method.
(Contributed by Raymond Hettinger.)</p>
</li>
<li><p class="first">An optional <tt class="docutils literal"><span class="pre">timeout</span></tt> parameter, specifying a timeout measured in
seconds, was added to the <a class="reference internal" href="../library/httplib.html#httplib.HTTPConnection" title="httplib.HTTPConnection"><tt class="xref py py-class docutils literal"><span class="pre">httplib.HTTPConnection</span></tt></a> and
<tt class="xref py py-class docutils literal"><span class="pre">HTTPSConnection</span></tt> class constructors.  (Added by Facundo
Batista.)</p>
</li>
<li><p class="first">Most of the <a class="reference internal" href="../library/inspect.html#module-inspect" title="inspect: Extract information and source code from live objects."><tt class="xref py py-mod docutils literal"><span class="pre">inspect</span></tt></a> module&#8217;s functions, such as
<tt class="xref py py-func docutils literal"><span class="pre">getmoduleinfo()</span></tt> and <tt class="xref py py-func docutils literal"><span class="pre">getargs()</span></tt>, now return named tuples.
In addition to behaving like tuples, the elements of the  return value
can also be accessed as attributes.
(Contributed by Raymond Hettinger.)</p>
<p>Some new functions in the module include
<tt class="xref py py-func docutils literal"><span class="pre">isgenerator()</span></tt>, <tt class="xref py py-func docutils literal"><span class="pre">isgeneratorfunction()</span></tt>,
and <tt class="xref py py-func docutils literal"><span class="pre">isabstract()</span></tt>.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><tt class="xref py py-mod docutils literal"><span class="pre">itertools</span></tt></a> module gained several new functions.</p>
<p><tt class="docutils literal"><span class="pre">izip_longest(iter1,</span> <span class="pre">iter2,</span> <span class="pre">...[,</span> <span class="pre">fillvalue])</span></tt> makes tuples from
each of the elements; if some of the iterables are shorter than
others, the missing values are set to <em>fillvalue</em>.  For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">tuple</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">izip_longest</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">]))</span>
<span class="go">((1, 1), (2, 2), (3, 3), (None, 4), (None, 5))</span>
</pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">product(iter1,</span> <span class="pre">iter2,</span> <span class="pre">...,</span> <span class="pre">[repeat=N])</span></tt> returns the Cartesian product
of the supplied iterables, a set of tuples containing
every possible combination of the elements returned from each iterable.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">]))</span>
<span class="go">[(1, 4), (1, 5), (1, 6),</span>
<span class="go"> (2, 4), (2, 5), (2, 6),</span>
<span class="go"> (3, 4), (3, 5), (3, 6)]</span>
</pre></div>
</div>
<p>The optional <em>repeat</em> keyword argument is used for taking the
product of an iterable or a set of iterables with themselves,
repeated <em>N</em> times.  With a single iterable argument, <em>N</em>-tuples
are returned:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">],</span> <span class="n">repeat</span><span class="o">=</span><span class="mi">3</span><span class="p">))</span>
<span class="go">[(1, 1, 1), (1, 1, 2), (1, 2, 1), (1, 2, 2),</span>
<span class="go"> (2, 1, 1), (2, 1, 2), (2, 2, 1), (2, 2, 2)]</span>
</pre></div>
</div>
<p>With two iterables, <em>2N</em>-tuples are returned.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">product</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">],</span> <span class="p">[</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],</span> <span class="n">repeat</span><span class="o">=</span><span class="mi">2</span><span class="p">))</span>
<span class="go">[(1, 3, 1, 3), (1, 3, 1, 4), (1, 3, 2, 3), (1, 3, 2, 4),</span>
<span class="go"> (1, 4, 1, 3), (1, 4, 1, 4), (1, 4, 2, 3), (1, 4, 2, 4),</span>
<span class="go"> (2, 3, 1, 3), (2, 3, 1, 4), (2, 3, 2, 3), (2, 3, 2, 4),</span>
<span class="go"> (2, 4, 1, 3), (2, 4, 1, 4), (2, 4, 2, 3), (2, 4, 2, 4)]</span>
</pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">combinations(iterable,</span> <span class="pre">r)</span></tt> returns sub-sequences of length <em>r</em> from
the elements of <em>iterable</em>.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">combinations</span><span class="p">(</span><span class="s">&#39;123&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">))</span>
<span class="go">[(&#39;1&#39;, &#39;2&#39;), (&#39;1&#39;, &#39;3&#39;), (&#39;2&#39;, &#39;3&#39;)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">combinations</span><span class="p">(</span><span class="s">&#39;123&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="go">[(&#39;1&#39;, &#39;2&#39;, &#39;3&#39;)]</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">combinations</span><span class="p">(</span><span class="s">&#39;1234&#39;</span><span class="p">,</span> <span class="mi">3</span><span class="p">))</span>
<span class="go">[(&#39;1&#39;, &#39;2&#39;, &#39;3&#39;), (&#39;1&#39;, &#39;2&#39;, &#39;4&#39;),</span>
<span class="go"> (&#39;1&#39;, &#39;3&#39;, &#39;4&#39;), (&#39;2&#39;, &#39;3&#39;, &#39;4&#39;)]</span>
</pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">permutations(iter[,</span> <span class="pre">r])</span></tt> returns all the permutations of length <em>r</em> of
the iterable&#8217;s elements.  If <em>r</em> is not specified, it will default to the
number of elements produced by the iterable.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">permutations</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">,</span><span class="mi">4</span><span class="p">],</span> <span class="mi">2</span><span class="p">))</span>
<span class="go">[(1, 2), (1, 3), (1, 4),</span>
<span class="go"> (2, 1), (2, 3), (2, 4),</span>
<span class="go"> (3, 1), (3, 2), (3, 4),</span>
<span class="go"> (4, 1), (4, 2), (4, 3)]</span>
</pre></div>
</div>
<p><tt class="docutils literal"><span class="pre">itertools.chain(*iterables)</span></tt> is an existing function in
<a class="reference internal" href="../library/itertools.html#module-itertools" title="itertools: Functions creating iterators for efficient looping."><tt class="xref py py-mod docutils literal"><span class="pre">itertools</span></tt></a> that gained a new constructor in Python 2.6.
<tt class="docutils literal"><span class="pre">itertools.chain.from_iterable(iterable)</span></tt> takes a single
iterable that should return other iterables.  <tt class="xref py py-func docutils literal"><span class="pre">chain()</span></tt> will
then return all the elements of the first iterable, then
all the elements of the second, and so on.</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="nb">list</span><span class="p">(</span><span class="n">itertools</span><span class="o">.</span><span class="n">chain</span><span class="o">.</span><span class="n">from_iterable</span><span class="p">([[</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">3</span><span class="p">],</span> <span class="p">[</span><span class="mi">4</span><span class="p">,</span><span class="mi">5</span><span class="p">,</span><span class="mi">6</span><span class="p">]]))</span>
<span class="go">[1, 2, 3, 4, 5, 6]</span>
</pre></div>
</div>
<p>(All contributed by Raymond Hettinger.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/logging.html#module-logging" title="logging: Flexible event logging system for applications."><tt class="xref py py-mod docutils literal"><span class="pre">logging</span></tt></a> module&#8217;s <tt class="xref py py-class docutils literal"><span class="pre">FileHandler</span></tt> class
and its subclasses <tt class="xref py py-class docutils literal"><span class="pre">WatchedFileHandler</span></tt>, <tt class="xref py py-class docutils literal"><span class="pre">RotatingFileHandler</span></tt>,
and <tt class="xref py py-class docutils literal"><span class="pre">TimedRotatingFileHandler</span></tt> now
have an optional <em>delay</em> parameter to their constructors.  If <em>delay</em>
is true, opening of the log file is deferred until the first
<tt class="xref py py-meth docutils literal"><span class="pre">emit()</span></tt> call is made.  (Contributed by Vinay Sajip.)</p>
<p><tt class="xref py py-class docutils literal"><span class="pre">TimedRotatingFileHandler</span></tt> also has a <em>utc</em> constructor
parameter.  If the argument is true, UTC time will be used
in determining when midnight occurs and in generating filenames;
otherwise local time will be used.</p>
</li>
<li><p class="first">Several new functions were added to the <a class="reference internal" href="../library/math.html#module-math" title="math: Mathematical functions (sin() etc.)."><tt class="xref py py-mod docutils literal"><span class="pre">math</span></tt></a> module:</p>
<ul class="simple">
<li><a class="reference internal" href="../library/math.html#math.isinf" title="math.isinf"><tt class="xref py py-func docutils literal"><span class="pre">isinf()</span></tt></a> and <a class="reference internal" href="../library/math.html#math.isnan" title="math.isnan"><tt class="xref py py-func docutils literal"><span class="pre">isnan()</span></tt></a> determine whether a given float
is a (positive or negative) infinity or a NaN (Not a Number), respectively.</li>
<li><a class="reference internal" href="../library/math.html#math.copysign" title="math.copysign"><tt class="xref py py-func docutils literal"><span class="pre">copysign()</span></tt></a> copies the sign bit of an IEEE 754 number,
returning the absolute value of <em>x</em> combined with the sign bit of
<em>y</em>.  For example, <tt class="docutils literal"><span class="pre">math.copysign(1,</span> <span class="pre">-0.0)</span></tt> returns -1.0.
(Contributed by Christian Heimes.)</li>
<li><a class="reference internal" href="../library/math.html#math.factorial" title="math.factorial"><tt class="xref py py-func docutils literal"><span class="pre">factorial()</span></tt></a> computes the factorial of a number.
(Contributed by Raymond Hettinger; <a class="reference external" href="http://bugs.python.org/issue2138">issue 2138</a>.)</li>
<li><a class="reference internal" href="../library/math.html#math.fsum" title="math.fsum"><tt class="xref py py-func docutils literal"><span class="pre">fsum()</span></tt></a> adds up the stream of numbers from an iterable,
and is careful to avoid loss of precision through using partial sums.
(Contributed by Jean Brouwers, Raymond Hettinger, and Mark Dickinson;
<a class="reference external" href="http://bugs.python.org/issue2819">issue 2819</a>.)</li>
<li><a class="reference internal" href="../library/math.html#math.acosh" title="math.acosh"><tt class="xref py py-func docutils literal"><span class="pre">acosh()</span></tt></a>, <a class="reference internal" href="../library/math.html#math.asinh" title="math.asinh"><tt class="xref py py-func docutils literal"><span class="pre">asinh()</span></tt></a>
and <a class="reference internal" href="../library/math.html#math.atanh" title="math.atanh"><tt class="xref py py-func docutils literal"><span class="pre">atanh()</span></tt></a> compute the inverse hyperbolic functions.</li>
<li><a class="reference internal" href="../library/math.html#math.log1p" title="math.log1p"><tt class="xref py py-func docutils literal"><span class="pre">log1p()</span></tt></a> returns the natural logarithm of <em>1+x</em>
(base <em>e</em>).</li>
<li><tt class="xref py py-func docutils literal"><span class="pre">trunc()</span></tt> rounds a number toward zero, returning the closest
<tt class="xref py py-class docutils literal"><span class="pre">Integral</span></tt> that&#8217;s between the function&#8217;s argument and zero.
Added as part of the backport of
<a class="reference external" href="#pep-3141">PEP 3141&#8217;s type hierarchy for numbers</a>.</li>
</ul>
</li>
<li><p class="first">The <a class="reference internal" href="../library/math.html#module-math" title="math: Mathematical functions (sin() etc.)."><tt class="xref py py-mod docutils literal"><span class="pre">math</span></tt></a> module has been improved to give more consistent
behaviour across platforms, especially with respect to handling of
floating-point exceptions and IEEE 754 special values.</p>
<p>Whenever possible, the module follows the recommendations of the C99
standard about 754&#8217;s special values.  For example, <tt class="docutils literal"><span class="pre">sqrt(-1.)</span></tt>
should now give a <a class="reference internal" href="../library/exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a> across almost all platforms,
while <tt class="docutils literal"><span class="pre">sqrt(float('NaN'))</span></tt> should return a NaN on all IEEE 754
platforms.  Where Annex &#8216;F&#8217; of the C99 standard recommends signaling
&#8216;divide-by-zero&#8217; or &#8216;invalid&#8217;, Python will raise <a class="reference internal" href="../library/exceptions.html#exceptions.ValueError" title="exceptions.ValueError"><tt class="xref py py-exc docutils literal"><span class="pre">ValueError</span></tt></a>.
Where Annex &#8216;F&#8217; of the C99 standard recommends signaling &#8216;overflow&#8217;,
Python will raise <a class="reference internal" href="../library/exceptions.html#exceptions.OverflowError" title="exceptions.OverflowError"><tt class="xref py py-exc docutils literal"><span class="pre">OverflowError</span></tt></a>.  (See <a class="reference external" href="http://bugs.python.org/issue711019">issue 711019</a> and
<a class="reference external" href="http://bugs.python.org/issue1640">issue 1640</a>.)</p>
<p>(Contributed by Christian Heimes and Mark Dickinson.)</p>
</li>
<li><p class="first"><a class="reference internal" href="../library/mmap.html#module-mmap" title="mmap: Interface to memory-mapped files for Unix and Windows."><tt class="xref py py-class docutils literal"><span class="pre">mmap</span></tt></a> objects now have a <tt class="xref py py-meth docutils literal"><span class="pre">rfind()</span></tt> method that searches for a
substring beginning at the end of the string and searching
backwards.  The <tt class="xref py py-meth docutils literal"><span class="pre">find()</span></tt> method also gained an <em>end</em> parameter
giving an index at which to stop searching.
(Contributed by John Lenton.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/operator.html#module-operator" title="operator: Functions corresponding to the standard operators."><tt class="xref py py-mod docutils literal"><span class="pre">operator</span></tt></a> module gained a
<tt class="xref py py-func docutils literal"><span class="pre">methodcaller()</span></tt> function that takes a name and an optional
set of arguments, returning a callable that will call
the named function on any arguments passed to it.  For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="c"># Equivalent to lambda s: s.replace(&#39;old&#39;, &#39;new&#39;)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">replacer</span> <span class="o">=</span> <span class="n">operator</span><span class="o">.</span><span class="n">methodcaller</span><span class="p">(</span><span class="s">&#39;replace&#39;</span><span class="p">,</span> <span class="s">&#39;old&#39;</span><span class="p">,</span> <span class="s">&#39;new&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">replacer</span><span class="p">(</span><span class="s">&#39;old wine in old bottles&#39;</span><span class="p">)</span>
<span class="go">&#39;new wine in new bottles&#39;</span>
</pre></div>
</div>
<p>(Contributed by Georg Brandl, after a suggestion by Gregory Petrosyan.)</p>
<p>The <tt class="xref py py-func docutils literal"><span class="pre">attrgetter()</span></tt> function now accepts dotted names and performs
the corresponding attribute lookups:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">inst_name</span> <span class="o">=</span> <span class="n">operator</span><span class="o">.</span><span class="n">attrgetter</span><span class="p">(</span>
<span class="gp">... </span>       <span class="s">&#39;__class__.__name__&#39;</span><span class="p">)</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">inst_name</span><span class="p">(</span><span class="s">&#39;&#39;</span><span class="p">)</span>
<span class="go">&#39;str&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">inst_name</span><span class="p">(</span><span class="n">help</span><span class="p">)</span>
<span class="go">&#39;_Helper&#39;</span>
</pre></div>
</div>
<p>(Contributed by Georg Brandl, after a suggestion by Barry Warsaw.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/os.html#module-os" title="os: Miscellaneous operating system interfaces."><tt class="xref py py-mod docutils literal"><span class="pre">os</span></tt></a> module now wraps several new system calls.
<tt class="docutils literal"><span class="pre">fchmod(fd,</span> <span class="pre">mode)</span></tt> and <tt class="docutils literal"><span class="pre">fchown(fd,</span> <span class="pre">uid,</span> <span class="pre">gid)</span></tt> change the mode
and ownership of an opened file, and <tt class="docutils literal"><span class="pre">lchmod(path,</span> <span class="pre">mode)</span></tt> changes
the mode of a symlink.  (Contributed by Georg Brandl and Christian
Heimes.)</p>
<p><tt class="xref py py-func docutils literal"><span class="pre">chflags()</span></tt> and <tt class="xref py py-func docutils literal"><span class="pre">lchflags()</span></tt> are wrappers for the
corresponding system calls (where they&#8217;re available), changing the
flags set on a file.  Constants for the flag values are defined in
the <a class="reference internal" href="../library/stat.html#module-stat" title="stat: Utilities for interpreting the results of os.stat(), os.lstat() and os.fstat()."><tt class="xref py py-mod docutils literal"><span class="pre">stat</span></tt></a> module; some possible values include
<tt class="xref py py-const docutils literal"><span class="pre">UF_IMMUTABLE</span></tt> to signal the file may not be changed and
<tt class="xref py py-const docutils literal"><span class="pre">UF_APPEND</span></tt> to indicate that data can only be appended to the
file.  (Contributed by M. Levinson.)</p>
<p><tt class="docutils literal"><span class="pre">os.closerange(low,</span> <span class="pre">high)</span></tt> efficiently closes all file descriptors
from <em>low</em> to <em>high</em>, ignoring any errors and not including <em>high</em> itself.
This function is now used by the <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><tt class="xref py py-mod docutils literal"><span class="pre">subprocess</span></tt></a> module to make starting
processes faster.  (Contributed by Georg Brandl; <a class="reference external" href="http://bugs.python.org/issue1663329">issue 1663329</a>.)</p>
</li>
<li><p class="first">The <tt class="docutils literal"><span class="pre">os.environ</span></tt> object&#8217;s <tt class="xref py py-meth docutils literal"><span class="pre">clear()</span></tt> method will now unset the
environment variables using <a class="reference internal" href="../library/os.html#os.unsetenv" title="os.unsetenv"><tt class="xref py py-func docutils literal"><span class="pre">os.unsetenv()</span></tt></a> in addition to clearing
the object&#8217;s keys.  (Contributed by Martin Horcicka; <a class="reference external" href="http://bugs.python.org/issue1181">issue 1181</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/os.html#os.walk" title="os.walk"><tt class="xref py py-func docutils literal"><span class="pre">os.walk()</span></tt></a> function now has a <tt class="docutils literal"><span class="pre">followlinks</span></tt> parameter. If
set to True, it will follow symlinks pointing to directories and
visit the directory&#8217;s contents.  For backward compatibility, the
parameter&#8217;s default value is false.  Note that the function can fall
into an infinite recursion if there&#8217;s a symlink that points to a
parent directory.  (<a class="reference external" href="http://bugs.python.org/issue1273829">issue 1273829</a>)</p>
</li>
<li><p class="first">In the <a class="reference internal" href="../library/os.path.html#module-os.path" title="os.path: Operations on pathnames."><tt class="xref py py-mod docutils literal"><span class="pre">os.path</span></tt></a> module, the <tt class="xref py py-func docutils literal"><span class="pre">splitext()</span></tt> function
has been changed to not split on leading period characters.
This produces better results when operating on Unix&#8217;s dot-files.
For example, <tt class="docutils literal"><span class="pre">os.path.splitext('.ipython')</span></tt>
now returns <tt class="docutils literal"><span class="pre">('.ipython',</span> <span class="pre">'')</span></tt> instead of <tt class="docutils literal"><span class="pre">('',</span> <span class="pre">'.ipython')</span></tt>.
(<a class="reference external" href="http://bugs.python.org/issue1115886">issue 1115886</a>)</p>
<p>A new function, <tt class="docutils literal"><span class="pre">os.path.relpath(path,</span> <span class="pre">start='.')</span></tt>, returns a relative path
from the <tt class="docutils literal"><span class="pre">start</span></tt> path, if it&#8217;s supplied, or from the current
working directory to the destination <tt class="docutils literal"><span class="pre">path</span></tt>.  (Contributed by
Richard Barran; <a class="reference external" href="http://bugs.python.org/issue1339796">issue 1339796</a>.)</p>
<p>On Windows, <a class="reference internal" href="../library/os.path.html#os.path.expandvars" title="os.path.expandvars"><tt class="xref py py-func docutils literal"><span class="pre">os.path.expandvars()</span></tt></a> will now expand environment variables
given in the form &#8220;%var%&#8221;, and &#8220;~user&#8221; will be expanded into the
user&#8217;s home directory path.  (Contributed by Josiah Carlson;
<a class="reference external" href="http://bugs.python.org/issue957650">issue 957650</a>.)</p>
</li>
<li><p class="first">The Python debugger provided by the <a class="reference internal" href="../library/pdb.html#module-pdb" title="pdb: The Python debugger for interactive interpreters."><tt class="xref py py-mod docutils literal"><span class="pre">pdb</span></tt></a> module
gained a new command: &#8220;run&#8221; restarts the Python program being debugged
and can optionally take new command-line arguments for the program.
(Contributed by Rocky Bernstein; <a class="reference external" href="http://bugs.python.org/issue1393667">issue 1393667</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/pdb.html#pdb.post_mortem" title="pdb.post_mortem"><tt class="xref py py-func docutils literal"><span class="pre">pdb.post_mortem()</span></tt></a> function, used to begin debugging a
traceback, will now use the traceback returned by <a class="reference internal" href="../library/sys.html#sys.exc_info" title="sys.exc_info"><tt class="xref py py-func docutils literal"><span class="pre">sys.exc_info()</span></tt></a>
if no traceback is supplied.   (Contributed by Facundo Batista;
<a class="reference external" href="http://bugs.python.org/issue1106316">issue 1106316</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/pickletools.html#module-pickletools" title="pickletools: Contains extensive comments about the pickle protocols and pickle-machine opcodes, as well as some useful functions."><tt class="xref py py-mod docutils literal"><span class="pre">pickletools</span></tt></a> module now has an <tt class="xref py py-func docutils literal"><span class="pre">optimize()</span></tt> function
that takes a string containing a pickle and removes some unused
opcodes, returning a shorter pickle that contains the same data structure.
(Contributed by Raymond Hettinger.)</p>
</li>
<li><p class="first">A <tt class="xref py py-func docutils literal"><span class="pre">get_data()</span></tt> function was added to the <a class="reference internal" href="../library/pkgutil.html#module-pkgutil" title="pkgutil: Utilities for the import system."><tt class="xref py py-mod docutils literal"><span class="pre">pkgutil</span></tt></a>
module that returns the contents of resource files included
with an installed Python package.  For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">pkgutil</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">pkgutil</span><span class="o">.</span><span class="n">get_data</span><span class="p">(</span><span class="s">&#39;test&#39;</span><span class="p">,</span> <span class="s">&#39;exception_hierarchy.txt&#39;</span><span class="p">)</span>
<span class="go">BaseException</span>
<span class="go"> +-- SystemExit</span>
<span class="go"> +-- KeyboardInterrupt</span>
<span class="go"> +-- GeneratorExit</span>
<span class="go"> +-- Exception</span>
<span class="go">      +-- StopIteration</span>
<span class="go">      +-- StandardError</span>
<span class="go"> ...</span>
</pre></div>
</div>
<p>(Contributed by Paul Moore; <a class="reference external" href="http://bugs.python.org/issue2439">issue 2439</a>.)</p>
</li>
<li><p class="first">The <tt class="xref py py-mod docutils literal"><span class="pre">pyexpat</span></tt> module&#8217;s <tt class="xref py py-class docutils literal"><span class="pre">Parser</span></tt> objects now allow setting
their <tt class="xref py py-attr docutils literal"><span class="pre">buffer_size</span></tt> attribute to change the size of the buffer
used to hold character data.
(Contributed by Achim Gaedke; <a class="reference external" href="http://bugs.python.org/issue1137">issue 1137</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/queue.html#module-Queue" title="Queue: A synchronized queue class."><tt class="xref py py-mod docutils literal"><span class="pre">Queue</span></tt></a> module now provides queue variants that retrieve entries
in different orders.  The <tt class="xref py py-class docutils literal"><span class="pre">PriorityQueue</span></tt> class stores
queued items in a heap and retrieves them in priority order,
and <tt class="xref py py-class docutils literal"><span class="pre">LifoQueue</span></tt> retrieves the most recently added entries first,
meaning that it behaves like a stack.
(Contributed by Raymond Hettinger.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/random.html#module-random" title="random: Generate pseudo-random numbers with various common distributions."><tt class="xref py py-mod docutils literal"><span class="pre">random</span></tt></a> module&#8217;s <tt class="xref py py-class docutils literal"><span class="pre">Random</span></tt> objects can
now be pickled on a 32-bit system and unpickled on a 64-bit
system, and vice versa.  Unfortunately, this change also means
that Python 2.6&#8217;s <tt class="xref py py-class docutils literal"><span class="pre">Random</span></tt> objects can&#8217;t be unpickled correctly
on earlier versions of Python.
(Contributed by Shawn Ligocki; <a class="reference external" href="http://bugs.python.org/issue1727780">issue 1727780</a>.)</p>
<p>The new <tt class="docutils literal"><span class="pre">triangular(low,</span> <span class="pre">high,</span> <span class="pre">mode)</span></tt> function returns random
numbers following a triangular distribution.   The returned values
are between <em>low</em> and <em>high</em>, not including <em>high</em> itself, and
with <em>mode</em> as the most frequently occurring value
in the distribution.  (Contributed by Wladmir van der Laan and
Raymond Hettinger; <a class="reference external" href="http://bugs.python.org/issue1681432">issue 1681432</a>.)</p>
</li>
<li><p class="first">Long regular expression searches carried out by the  <a class="reference internal" href="../library/re.html#module-re" title="re: Regular expression operations."><tt class="xref py py-mod docutils literal"><span class="pre">re</span></tt></a>
module will check for signals being delivered, so
time-consuming searches can now be interrupted.
(Contributed by Josh Hoyt and Ralf Schmitt; <a class="reference external" href="http://bugs.python.org/issue846388">issue 846388</a>.)</p>
<p>The regular expression module is implemented by compiling bytecodes
for a tiny regex-specific virtual machine.  Untrusted code
could create malicious strings of bytecode directly and cause crashes,
so Python 2.6 includes a verifier for the regex bytecode.
(Contributed by Guido van Rossum from work for Google App Engine;
<a class="reference external" href="http://bugs.python.org/issue3487">issue 3487</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/rlcompleter.html#module-rlcompleter" title="rlcompleter: Python identifier completion, suitable for the GNU readline library."><tt class="xref py py-mod docutils literal"><span class="pre">rlcompleter</span></tt></a> module&#8217;s <tt class="xref py py-meth docutils literal"><span class="pre">Completer.complete()</span></tt> method
will now ignore exceptions triggered while evaluating a name.
(Fixed by Lorenz Quack; <a class="reference external" href="http://bugs.python.org/issue2250">issue 2250</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/sched.html#module-sched" title="sched: General purpose event scheduler."><tt class="xref py py-mod docutils literal"><span class="pre">sched</span></tt></a> module&#8217;s <tt class="xref py py-class docutils literal"><span class="pre">scheduler</span></tt> instances now
have a read-only <tt class="xref py py-attr docutils literal"><span class="pre">queue</span></tt> attribute that returns the
contents of the scheduler&#8217;s queue, represented as a list of
named tuples with the fields <tt class="docutils literal"><span class="pre">(time,</span> <span class="pre">priority,</span> <span class="pre">action,</span> <span class="pre">argument)</span></tt>.
(Contributed by Raymond Hettinger; <a class="reference external" href="http://bugs.python.org/issue1861">issue 1861</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/select.html#module-select" title="select: Wait for I/O completion on multiple streams."><tt class="xref py py-mod docutils literal"><span class="pre">select</span></tt></a> module now has wrapper functions
for the Linux <tt class="xref c c-func docutils literal"><span class="pre">epoll()</span></tt> and BSD <tt class="xref c c-func docutils literal"><span class="pre">kqueue()</span></tt> system calls.
<tt class="xref py py-meth docutils literal"><span class="pre">modify()</span></tt> method was added to the existing <tt class="xref py py-class docutils literal"><span class="pre">poll</span></tt>
objects; <tt class="docutils literal"><span class="pre">pollobj.modify(fd,</span> <span class="pre">eventmask)</span></tt> takes a file descriptor
or file object and an event mask, modifying the recorded event mask
for that file.
(Contributed by Christian Heimes; <a class="reference external" href="http://bugs.python.org/issue1657">issue 1657</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/shutil.html#shutil.copytree" title="shutil.copytree"><tt class="xref py py-func docutils literal"><span class="pre">shutil.copytree()</span></tt></a> function now has an optional <em>ignore</em> argument
that takes a callable object.  This callable will receive each directory path
and a list of the directory&#8217;s contents, and returns a list of names that
will be ignored, not copied.</p>
<p>The <a class="reference internal" href="../library/shutil.html#module-shutil" title="shutil: High-level file operations, including copying."><tt class="xref py py-mod docutils literal"><span class="pre">shutil</span></tt></a> module also provides an <tt class="xref py py-func docutils literal"><span class="pre">ignore_patterns()</span></tt>
function for use with this new parameter.  <tt class="xref py py-func docutils literal"><span class="pre">ignore_patterns()</span></tt>
takes an arbitrary number of glob-style patterns and returns a
callable that will ignore any files and directories that match any
of these patterns.  The following example copies a directory tree,
but skips both <tt class="file docutils literal"><span class="pre">.svn</span></tt> directories and Emacs backup files,
which have names ending with &#8216;~&#8217;:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">shutil</span><span class="o">.</span><span class="n">copytree</span><span class="p">(</span><span class="s">&#39;Doc/library&#39;</span><span class="p">,</span> <span class="s">&#39;/tmp/library&#39;</span><span class="p">,</span>
                <span class="n">ignore</span><span class="o">=</span><span class="n">shutil</span><span class="o">.</span><span class="n">ignore_patterns</span><span class="p">(</span><span class="s">&#39;*~&#39;</span><span class="p">,</span> <span class="s">&#39;.svn&#39;</span><span class="p">))</span>
</pre></div>
</div>
<p>(Contributed by Tarek Ziadé; <a class="reference external" href="http://bugs.python.org/issue2663">issue 2663</a>.)</p>
</li>
<li><p class="first">Integrating signal handling with GUI handling event loops
like those used by Tkinter or GTk+ has long been a problem; most
software ends up polling, waking up every fraction of a second to check
if any GUI events have occurred.
The <a class="reference internal" href="../library/signal.html#module-signal" title="signal: Set handlers for asynchronous events."><tt class="xref py py-mod docutils literal"><span class="pre">signal</span></tt></a> module can now make this more efficient.
Calling <tt class="docutils literal"><span class="pre">signal.set_wakeup_fd(fd)</span></tt> sets a file descriptor
to be used; when a signal is received, a byte is written to that
file descriptor.  There&#8217;s also a C-level function,
<a class="reference internal" href="../c-api/exceptions.html#PySignal_SetWakeupFd" title="PySignal_SetWakeupFd"><tt class="xref c c-func docutils literal"><span class="pre">PySignal_SetWakeupFd()</span></tt></a>, for setting the descriptor.</p>
<p>Event loops will use this by opening a pipe to create two descriptors,
one for reading and one for writing.  The writable descriptor
will be passed to <tt class="xref py py-func docutils literal"><span class="pre">set_wakeup_fd()</span></tt>, and the readable descriptor
will be added to the list of descriptors monitored by the event loop via
<tt class="xref c c-func docutils literal"><span class="pre">select()</span></tt> or <tt class="xref c c-func docutils literal"><span class="pre">poll()</span></tt>.
On receiving a signal, a byte will be written and the main event loop
will be woken up, avoiding the need to poll.</p>
<p>(Contributed by Adam Olsen; <a class="reference external" href="http://bugs.python.org/issue1583">issue 1583</a>.)</p>
<p>The <tt class="xref py py-func docutils literal"><span class="pre">siginterrupt()</span></tt> function is now available from Python code,
and allows changing whether signals can interrupt system calls or not.
(Contributed by Ralf Schmitt.)</p>
<p>The <tt class="xref py py-func docutils literal"><span class="pre">setitimer()</span></tt> and <tt class="xref py py-func docutils literal"><span class="pre">getitimer()</span></tt> functions have also been
added (where they&#8217;re available).  <tt class="xref py py-func docutils literal"><span class="pre">setitimer()</span></tt>
allows setting interval timers that will cause a signal to be
delivered to the process after a specified time, measured in
wall-clock time, consumed process time, or combined process+system
time.  (Contributed by Guilherme Polo; <a class="reference external" href="http://bugs.python.org/issue2240">issue 2240</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/smtplib.html#module-smtplib" title="smtplib: SMTP protocol client (requires sockets)."><tt class="xref py py-mod docutils literal"><span class="pre">smtplib</span></tt></a> module now supports SMTP over SSL thanks to the
addition of the <tt class="xref py py-class docutils literal"><span class="pre">SMTP_SSL</span></tt> class. This class supports an
interface identical to the existing <tt class="xref py py-class docutils literal"><span class="pre">SMTP</span></tt> class.
(Contributed by Monty Taylor.)  Both class constructors also have an
optional <tt class="docutils literal"><span class="pre">timeout</span></tt> parameter that specifies a timeout for the
initial connection attempt, measured in seconds.  (Contributed by
Facundo Batista.)</p>
<p>An implementation of the LMTP protocol (<span class="target" id="index-22"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc2033.html"><strong>RFC 2033</strong></a>) was also added
to the module.  LMTP is used in place of SMTP when transferring
e-mail between agents that don&#8217;t manage a mail queue.  (LMTP
implemented by Leif Hedstrom; <a class="reference external" href="http://bugs.python.org/issue957003">issue 957003</a>.)</p>
<p><tt class="xref py py-meth docutils literal"><span class="pre">SMTP.starttls()</span></tt> now complies with <span class="target" id="index-23"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc3207.html"><strong>RFC 3207</strong></a> and forgets any
knowledge obtained from the server not obtained from the TLS
negotiation itself.  (Patch contributed by Bill Fenner;
<a class="reference external" href="http://bugs.python.org/issue829951">issue 829951</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><tt class="xref py py-mod docutils literal"><span class="pre">socket</span></tt></a> module now supports TIPC (<a class="reference external" href="http://tipc.sf.net">http://tipc.sf.net</a>),
a high-performance non-IP-based protocol designed for use in clustered
environments.  TIPC addresses are 4- or 5-tuples.
(Contributed by Alberto Bertogli; <a class="reference external" href="http://bugs.python.org/issue1646">issue 1646</a>.)</p>
<p>A new function, <tt class="xref py py-func docutils literal"><span class="pre">create_connection()</span></tt>, takes an address and
connects to it using an optional timeout value, returning the
connected socket object.  This function also looks up the address&#8217;s
type and connects to it using IPv4 or IPv6 as appropriate.  Changing
your code to use <tt class="xref py py-func docutils literal"><span class="pre">create_connection()</span></tt> instead of
<tt class="docutils literal"><span class="pre">socket(socket.AF_INET,</span> <span class="pre">...)</span></tt> may be all that&#8217;s required to make
your code work with IPv6.</p>
</li>
<li><p class="first">The base classes in the <a class="reference internal" href="../library/socketserver.html#module-SocketServer" title="SocketServer: A framework for network servers."><tt class="xref py py-mod docutils literal"><span class="pre">SocketServer</span></tt></a> module now support
calling a <tt class="xref py py-meth docutils literal"><span class="pre">handle_timeout()</span></tt> method after a span of inactivity
specified by the server&#8217;s <tt class="xref py py-attr docutils literal"><span class="pre">timeout</span></tt> attribute.  (Contributed
by Michael Pomraning.)  The <tt class="xref py py-meth docutils literal"><span class="pre">serve_forever()</span></tt> method
now takes an optional poll interval measured in seconds,
controlling how often the server will check for a shutdown request.
(Contributed by Pedro Werneck and Jeffrey Yasskin;
<a class="reference external" href="http://bugs.python.org/issue742598">issue 742598</a>, <a class="reference external" href="http://bugs.python.org/issue1193577">issue 1193577</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/sqlite3.html#module-sqlite3" title="sqlite3: A DB-API 2.0 implementation using SQLite 3.x."><tt class="xref py py-mod docutils literal"><span class="pre">sqlite3</span></tt></a> module, maintained by Gerhard Haering,
has been updated from version 2.3.2 in Python 2.5 to
version 2.4.1.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/struct.html#module-struct" title="struct: Interpret strings as packed binary data."><tt class="xref py py-mod docutils literal"><span class="pre">struct</span></tt></a> module now supports the C99 <tt class="xref c c-type docutils literal"><span class="pre">_Bool</span></tt> type,
using the format character <tt class="docutils literal"><span class="pre">'?'</span></tt>.
(Contributed by David Remahl.)</p>
</li>
<li><p class="first">The <tt class="xref py py-class docutils literal"><span class="pre">Popen</span></tt> objects provided by the <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><tt class="xref py py-mod docutils literal"><span class="pre">subprocess</span></tt></a> module
now have <tt class="xref py py-meth docutils literal"><span class="pre">terminate()</span></tt>, <tt class="xref py py-meth docutils literal"><span class="pre">kill()</span></tt>, and <tt class="xref py py-meth docutils literal"><span class="pre">send_signal()</span></tt> methods.
On Windows, <tt class="xref py py-meth docutils literal"><span class="pre">send_signal()</span></tt> only supports the <tt class="xref py py-const docutils literal"><span class="pre">SIGTERM</span></tt>
signal, and all these methods are aliases for the Win32 API function
<tt class="xref c c-func docutils literal"><span class="pre">TerminateProcess()</span></tt>.
(Contributed by Christian Heimes.)</p>
</li>
<li><p class="first">A new variable in the <a class="reference internal" href="../library/sys.html#module-sys" title="sys: Access system-specific parameters and functions."><tt class="xref py py-mod docutils literal"><span class="pre">sys</span></tt></a> module, <tt class="xref py py-attr docutils literal"><span class="pre">float_info</span></tt>, is an
object containing information derived from the <tt class="file docutils literal"><span class="pre">float.h</span></tt> file
about the platform&#8217;s floating-point support.  Attributes of this
object include <tt class="xref py py-attr docutils literal"><span class="pre">mant_dig</span></tt> (number of digits in the mantissa),
<tt class="xref py py-attr docutils literal"><span class="pre">epsilon</span></tt> (smallest difference between 1.0 and the next
largest value representable), and several others.  (Contributed by
Christian Heimes; <a class="reference external" href="http://bugs.python.org/issue1534">issue 1534</a>.)</p>
<p>Another new variable, <tt class="xref py py-attr docutils literal"><span class="pre">dont_write_bytecode</span></tt>, controls whether Python
writes any <tt class="file docutils literal"><span class="pre">.pyc</span></tt> or <tt class="file docutils literal"><span class="pre">.pyo</span></tt> files on importing a module.
If this variable is true, the compiled files are not written.  The
variable is initially set on start-up by supplying the <a class="reference internal" href="../using/cmdline.html#cmdoption-B"><em class="xref std std-option">-B</em></a>
switch to the Python interpreter, or by setting the
<span class="target" id="index-24"></span><a class="reference internal" href="../using/cmdline.html#envvar-PYTHONDONTWRITEBYTECODE"><tt class="xref std std-envvar docutils literal"><span class="pre">PYTHONDONTWRITEBYTECODE</span></tt></a> environment variable before
running the interpreter.  Python code can subsequently
change the value of this variable to control whether bytecode files
are written or not.
(Contributed by Neal Norwitz and Georg Brandl.)</p>
<p>Information about the command-line arguments supplied to the Python
interpreter is available by reading attributes of a named
tuple available as <tt class="docutils literal"><span class="pre">sys.flags</span></tt>.  For example, the <tt class="xref py py-attr docutils literal"><span class="pre">verbose</span></tt>
attribute is true if Python
was executed in verbose mode, <tt class="xref py py-attr docutils literal"><span class="pre">debug</span></tt> is true in debugging mode, etc.
These attributes are all read-only.
(Contributed by Christian Heimes.)</p>
<p>A new function, <tt class="xref py py-func docutils literal"><span class="pre">getsizeof()</span></tt>, takes a Python object and returns
the amount of memory used by the object, measured in bytes.  Built-in
objects return correct results; third-party extensions may not,
but can define a <tt class="xref py py-meth docutils literal"><span class="pre">__sizeof__()</span></tt> method to return the
object&#8217;s size.
(Contributed by Robert Schuppenies; <a class="reference external" href="http://bugs.python.org/issue2898">issue 2898</a>.)</p>
<p>It&#8217;s now possible to determine the current profiler and tracer functions
by calling <a class="reference internal" href="../library/sys.html#sys.getprofile" title="sys.getprofile"><tt class="xref py py-func docutils literal"><span class="pre">sys.getprofile()</span></tt></a> and <a class="reference internal" href="../library/sys.html#sys.gettrace" title="sys.gettrace"><tt class="xref py py-func docutils literal"><span class="pre">sys.gettrace()</span></tt></a>.
(Contributed by Georg Brandl; <a class="reference external" href="http://bugs.python.org/issue1648">issue 1648</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/tarfile.html#module-tarfile" title="tarfile: Read and write tar-format archive files."><tt class="xref py py-mod docutils literal"><span class="pre">tarfile</span></tt></a> module now supports POSIX.1-2001 (pax) tarfiles in
addition to the POSIX.1-1988 (ustar) and GNU tar formats that were
already supported.  The default format is GNU tar; specify the
<tt class="docutils literal"><span class="pre">format</span></tt> parameter to open a file using a different format:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">tar</span> <span class="o">=</span> <span class="n">tarfile</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s">&quot;output.tar&quot;</span><span class="p">,</span> <span class="s">&quot;w&quot;</span><span class="p">,</span>
                   <span class="n">format</span><span class="o">=</span><span class="n">tarfile</span><span class="o">.</span><span class="n">PAX_FORMAT</span><span class="p">)</span>
</pre></div>
</div>
<p>The new <tt class="docutils literal"><span class="pre">encoding</span></tt> and <tt class="docutils literal"><span class="pre">errors</span></tt> parameters specify an encoding and
an error handling scheme for character conversions.  <tt class="docutils literal"><span class="pre">'strict'</span></tt>,
<tt class="docutils literal"><span class="pre">'ignore'</span></tt>, and <tt class="docutils literal"><span class="pre">'replace'</span></tt> are the three standard ways Python can
handle errors,;
<tt class="docutils literal"><span class="pre">'utf-8'</span></tt> is a special value that replaces bad characters with
their UTF-8 representation.  (Character conversions occur because the
PAX format supports Unicode filenames, defaulting to UTF-8 encoding.)</p>
<p>The <tt class="xref py py-meth docutils literal"><span class="pre">TarFile.add()</span></tt> method now accepts an <tt class="docutils literal"><span class="pre">exclude</span></tt> argument that&#8217;s
a function that can be used to exclude certain filenames from
an archive.
The function must take a filename and return true if the file
should be excluded or false if it should be archived.
The function is applied to both the name initially passed to <tt class="xref py py-meth docutils literal"><span class="pre">add()</span></tt>
and to the names of files in recursively-added directories.</p>
<p>(All changes contributed by Lars Gustäbel).</p>
</li>
<li><p class="first">An optional <tt class="docutils literal"><span class="pre">timeout</span></tt> parameter was added to the
<a class="reference internal" href="../library/telnetlib.html#telnetlib.Telnet" title="telnetlib.Telnet"><tt class="xref py py-class docutils literal"><span class="pre">telnetlib.Telnet</span></tt></a> class constructor, specifying a timeout
measured in seconds.  (Added by Facundo Batista.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/tempfile.html#tempfile.NamedTemporaryFile" title="tempfile.NamedTemporaryFile"><tt class="xref py py-class docutils literal"><span class="pre">tempfile.NamedTemporaryFile</span></tt></a> class usually deletes
the temporary file it created when the file is closed.  This
behaviour can now be changed by passing <tt class="docutils literal"><span class="pre">delete=False</span></tt> to the
constructor.  (Contributed by Damien Miller; <a class="reference external" href="http://bugs.python.org/issue1537850">issue 1537850</a>.)</p>
<p>A new class, <tt class="xref py py-class docutils literal"><span class="pre">SpooledTemporaryFile</span></tt>, behaves like
a temporary file but stores its data in memory until a maximum size is
exceeded.  On reaching that limit, the contents will be written to
an on-disk temporary file.  (Contributed by Dustin J. Mitchell.)</p>
<p>The <tt class="xref py py-class docutils literal"><span class="pre">NamedTemporaryFile</span></tt> and <tt class="xref py py-class docutils literal"><span class="pre">SpooledTemporaryFile</span></tt> classes
both work as context managers, so you can write
<tt class="docutils literal"><span class="pre">with</span> <span class="pre">tempfile.NamedTemporaryFile()</span> <span class="pre">as</span> <span class="pre">tmp:</span> <span class="pre">...</span></tt>.
(Contributed by Alexander Belopolsky; <a class="reference external" href="http://bugs.python.org/issue2021">issue 2021</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/test.html#module-test.test_support" title="test.test_support: Support for Python regression tests."><tt class="xref py py-mod docutils literal"><span class="pre">test.test_support</span></tt></a> module gained a number
of context managers useful for writing tests.
<tt class="xref py py-func docutils literal"><span class="pre">EnvironmentVarGuard()</span></tt> is a
context manager that temporarily changes environment variables and
automatically restores them to their old values.</p>
<p>Another context manager, <tt class="xref py py-class docutils literal"><span class="pre">TransientResource</span></tt>, can surround calls
to resources that may or may not be available; it will catch and
ignore a specified list of exceptions.  For example,
a network test may ignore certain failures when connecting to an
external web site:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">with</span> <span class="n">test_support</span><span class="o">.</span><span class="n">TransientResource</span><span class="p">(</span><span class="ne">IOError</span><span class="p">,</span>
                                <span class="n">errno</span><span class="o">=</span><span class="n">errno</span><span class="o">.</span><span class="n">ETIMEDOUT</span><span class="p">):</span>
    <span class="n">f</span> <span class="o">=</span> <span class="n">urllib</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s">&#39;https://sf.net&#39;</span><span class="p">)</span>
    <span class="o">...</span>
</pre></div>
</div>
<p>Finally, <tt class="xref py py-func docutils literal"><span class="pre">check_warnings()</span></tt> resets the <tt class="xref py py-mod docutils literal"><span class="pre">warning</span></tt> module&#8217;s
warning filters and returns an object that will record all warning
messages triggered (<a class="reference external" href="http://bugs.python.org/issue3781">issue 3781</a>):</p>
<div class="highlight-python"><div class="highlight"><pre><span class="k">with</span> <span class="n">test_support</span><span class="o">.</span><span class="n">check_warnings</span><span class="p">()</span> <span class="k">as</span> <span class="n">wrec</span><span class="p">:</span>
    <span class="n">warnings</span><span class="o">.</span><span class="n">simplefilter</span><span class="p">(</span><span class="s">&quot;always&quot;</span><span class="p">)</span>
    <span class="c"># ... code that triggers a warning ...</span>
    <span class="k">assert</span> <span class="nb">str</span><span class="p">(</span><span class="n">wrec</span><span class="o">.</span><span class="n">message</span><span class="p">)</span> <span class="o">==</span> <span class="s">&quot;function is outdated&quot;</span>
    <span class="k">assert</span> <span class="nb">len</span><span class="p">(</span><span class="n">wrec</span><span class="o">.</span><span class="n">warnings</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">,</span> <span class="s">&quot;Multiple warnings raised&quot;</span>
</pre></div>
</div>
<p>(Contributed by Brett Cannon.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/textwrap.html#module-textwrap" title="textwrap: Text wrapping and filling"><tt class="xref py py-mod docutils literal"><span class="pre">textwrap</span></tt></a> module can now preserve existing whitespace
at the beginnings and ends of the newly-created lines
by specifying <tt class="docutils literal"><span class="pre">drop_whitespace=False</span></tt>
as an argument:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">S</span> <span class="o">=</span> <span class="s">&quot;&quot;&quot;This  sentence  has a bunch   of</span>
<span class="gp">... </span><span class="s">  extra   whitespace.&quot;&quot;&quot;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">textwrap</span><span class="o">.</span><span class="n">fill</span><span class="p">(</span><span class="n">S</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">15</span><span class="p">)</span>
<span class="go">This  sentence</span>
<span class="go">has a bunch</span>
<span class="go">of    extra</span>
<span class="go">whitespace.</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">textwrap</span><span class="o">.</span><span class="n">fill</span><span class="p">(</span><span class="n">S</span><span class="p">,</span> <span class="n">drop_whitespace</span><span class="o">=</span><span class="bp">False</span><span class="p">,</span> <span class="n">width</span><span class="o">=</span><span class="mi">15</span><span class="p">)</span>
<span class="go">This  sentence</span>
<span class="go">  has a bunch</span>
<span class="go">   of    extra</span>
<span class="go">   whitespace.</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>(Contributed by Dwayne Bailey; <a class="reference external" href="http://bugs.python.org/issue1581073">issue 1581073</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Higher-level threading interface."><tt class="xref py py-mod docutils literal"><span class="pre">threading</span></tt></a> module API is being changed to use properties
such as <tt class="xref py py-attr docutils literal"><span class="pre">daemon</span></tt> instead of <tt class="xref py py-meth docutils literal"><span class="pre">setDaemon()</span></tt> and
<tt class="xref py py-meth docutils literal"><span class="pre">isDaemon()</span></tt> methods, and some methods have been renamed to use
underscores instead of camel-case; for example, the
<tt class="xref py py-meth docutils literal"><span class="pre">activeCount()</span></tt> method is renamed to <tt class="xref py py-meth docutils literal"><span class="pre">active_count()</span></tt>.  Both
the 2.6 and 3.0 versions of the module support the same properties
and renamed methods, but don&#8217;t remove the old methods.  No date has been set
for the deprecation of the old APIs in Python 3.x; the old APIs won&#8217;t
be removed in any 2.x version.
(Carried out by several people, most notably Benjamin Peterson.)</p>
<p>The <a class="reference internal" href="../library/threading.html#module-threading" title="threading: Higher-level threading interface."><tt class="xref py py-mod docutils literal"><span class="pre">threading</span></tt></a> module&#8217;s <tt class="xref py py-class docutils literal"><span class="pre">Thread</span></tt> objects
gained an <tt class="xref py py-attr docutils literal"><span class="pre">ident</span></tt> property that returns the thread&#8217;s
identifier, a nonzero integer.  (Contributed by Gregory P. Smith;
<a class="reference external" href="http://bugs.python.org/issue2871">issue 2871</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/timeit.html#module-timeit" title="timeit: Measure the execution time of small code snippets."><tt class="xref py py-mod docutils literal"><span class="pre">timeit</span></tt></a> module now accepts callables as well as strings
for the statement being timed and for the setup code.
Two convenience functions were added for creating
<tt class="xref py py-class docutils literal"><span class="pre">Timer</span></tt> instances:
<tt class="docutils literal"><span class="pre">repeat(stmt,</span> <span class="pre">setup,</span> <span class="pre">time,</span> <span class="pre">repeat,</span> <span class="pre">number)</span></tt> and
<tt class="docutils literal"><span class="pre">timeit(stmt,</span> <span class="pre">setup,</span> <span class="pre">time,</span> <span class="pre">number)</span></tt> create an instance and call
the corresponding method. (Contributed by Erik Demaine;
<a class="reference external" href="http://bugs.python.org/issue1533909">issue 1533909</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/tkinter.html#module-Tkinter" title="Tkinter: Interface to Tcl/Tk for graphical user interfaces"><tt class="xref py py-mod docutils literal"><span class="pre">Tkinter</span></tt></a> module now accepts lists and tuples for options,
separating the elements by spaces before passing the resulting value to
Tcl/Tk.
(Contributed by Guilherme Polo; <a class="reference external" href="http://bugs.python.org/issue2906">issue 2906</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/turtle.html#module-turtle" title="turtle: Turtle graphics for Tk"><tt class="xref py py-mod docutils literal"><span class="pre">turtle</span></tt></a> module for turtle graphics was greatly enhanced by
Gregor Lingl.  New features in the module include:</p>
<ul class="simple">
<li>Better animation of turtle movement and rotation.</li>
<li>Control over turtle movement using the new <tt class="xref py py-meth docutils literal"><span class="pre">delay()</span></tt>,
<tt class="xref py py-meth docutils literal"><span class="pre">tracer()</span></tt>, and <tt class="xref py py-meth docutils literal"><span class="pre">speed()</span></tt> methods.</li>
<li>The ability to set new shapes for the turtle, and to
define a new coordinate system.</li>
<li>Turtles now have an <tt class="xref py py-meth docutils literal"><span class="pre">undo()</span></tt> method that can roll back actions.</li>
<li>Simple support for reacting to input events such as mouse and keyboard
activity, making it possible to write simple games.</li>
<li>A <tt class="file docutils literal"><span class="pre">turtle.cfg</span></tt> file can be used to customize the starting appearance
of the turtle&#8217;s screen.</li>
<li>The module&#8217;s docstrings can be replaced by new docstrings that have been
translated into another language.</li>
</ul>
<p>(<a class="reference external" href="http://bugs.python.org/issue1513695">issue 1513695</a>)</p>
</li>
<li><p class="first">An optional <tt class="docutils literal"><span class="pre">timeout</span></tt> parameter was added to the
<a class="reference internal" href="../library/urllib.html#urllib.urlopen" title="urllib.urlopen"><tt class="xref py py-func docutils literal"><span class="pre">urllib.urlopen()</span></tt></a> function and the
<tt class="xref py py-class docutils literal"><span class="pre">urllib.ftpwrapper</span></tt> class constructor, as well as the
<a class="reference internal" href="../library/urllib2.html#urllib2.urlopen" title="urllib2.urlopen"><tt class="xref py py-func docutils literal"><span class="pre">urllib2.urlopen()</span></tt></a> function.  The parameter specifies a timeout
measured in seconds.   For example:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">u</span> <span class="o">=</span> <span class="n">urllib2</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s">&quot;http://slow.example.com&quot;</span><span class="p">,</span>
<span class="go">                        timeout=3)</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="nc">urllib2.URLError</span>: <span class="n-Identifier">&lt;urlopen error timed out&gt;</span>
<span class="go">&gt;&gt;&gt;</span>
</pre></div>
</div>
<p>(Added by Facundo Batista.)</p>
</li>
<li><p class="first">The Unicode database provided by the <a class="reference internal" href="../library/unicodedata.html#module-unicodedata" title="unicodedata: Access the Unicode Database."><tt class="xref py py-mod docutils literal"><span class="pre">unicodedata</span></tt></a> module
has been updated to version 5.1.0.  (Updated by
Martin von Loewis; <a class="reference external" href="http://bugs.python.org/issue3811">issue 3811</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><tt class="xref py py-mod docutils literal"><span class="pre">warnings</span></tt></a> module&#8217;s <tt class="xref py py-func docutils literal"><span class="pre">formatwarning()</span></tt> and <tt class="xref py py-func docutils literal"><span class="pre">showwarning()</span></tt>
gained an optional <em>line</em> argument that can be used to supply the
line of source code.  (Added as part of <a class="reference external" href="http://bugs.python.org/issue1631171">issue 1631171</a>, which re-implemented
part of the <a class="reference internal" href="../library/warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><tt class="xref py py-mod docutils literal"><span class="pre">warnings</span></tt></a> module in C code.)</p>
<p>A new function, <tt class="xref py py-func docutils literal"><span class="pre">catch_warnings()</span></tt>, is a context manager
intended for testing purposes that lets you temporarily modify the
warning filters and then restore their original values (<a class="reference external" href="http://bugs.python.org/issue3781">issue 3781</a>).</p>
</li>
<li><p class="first">The XML-RPC <a class="reference internal" href="../library/simplexmlrpcserver.html#module-SimpleXMLRPCServer" title="SimpleXMLRPCServer: Basic XML-RPC server implementation."><tt class="xref py py-class docutils literal"><span class="pre">SimpleXMLRPCServer</span></tt></a> and <a class="reference internal" href="../library/docxmlrpcserver.html#module-DocXMLRPCServer" title="DocXMLRPCServer: Self-documenting XML-RPC server implementation."><tt class="xref py py-class docutils literal"><span class="pre">DocXMLRPCServer</span></tt></a>
classes can now be prevented from immediately opening and binding to
their socket by passing True as the <tt class="docutils literal"><span class="pre">bind_and_activate</span></tt>
constructor parameter.  This can be used to modify the instance&#8217;s
<tt class="xref py py-attr docutils literal"><span class="pre">allow_reuse_address</span></tt> attribute before calling the
<tt class="xref py py-meth docutils literal"><span class="pre">server_bind()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">server_activate()</span></tt> methods to
open the socket and begin listening for connections.
(Contributed by Peter Parente; <a class="reference external" href="http://bugs.python.org/issue1599845">issue 1599845</a>.)</p>
<p><a class="reference internal" href="../library/simplexmlrpcserver.html#module-SimpleXMLRPCServer" title="SimpleXMLRPCServer: Basic XML-RPC server implementation."><tt class="xref py py-class docutils literal"><span class="pre">SimpleXMLRPCServer</span></tt></a> also has a <tt class="xref py py-attr docutils literal"><span class="pre">_send_traceback_header</span></tt>
attribute; if true, the exception and formatted traceback are returned
as HTTP headers &#8220;X-Exception&#8221; and &#8220;X-Traceback&#8221;.  This feature is
for debugging purposes only and should not be used on production servers
because the tracebacks might reveal passwords or other sensitive
information.  (Contributed by Alan McIntyre as part of his
project for Google&#8217;s Summer of Code 2007.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/xmlrpclib.html#module-xmlrpclib" title="xmlrpclib: XML-RPC client access."><tt class="xref py py-mod docutils literal"><span class="pre">xmlrpclib</span></tt></a> module no longer automatically converts
<a class="reference internal" href="../library/datetime.html#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">datetime.date</span></tt></a> and <a class="reference internal" href="../library/datetime.html#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">datetime.time</span></tt></a> to the
<tt class="xref py py-class docutils literal"><span class="pre">xmlrpclib.DateTime</span></tt> type; the conversion semantics were
not necessarily correct for all applications.  Code using
<a class="reference internal" href="../library/xmlrpclib.html#module-xmlrpclib" title="xmlrpclib: XML-RPC client access."><tt class="xref py py-mod docutils literal"><span class="pre">xmlrpclib</span></tt></a> should convert <tt class="xref py py-class docutils literal"><span class="pre">date</span></tt> and <a class="reference internal" href="../library/time.html#module-time" title="time: Time access and conversions."><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a>
instances. (<a class="reference external" href="http://bugs.python.org/issue1330538">issue 1330538</a>)  The code can also handle
dates before 1900 (contributed by Ralf Schmitt; <a class="reference external" href="http://bugs.python.org/issue2014">issue 2014</a>)
and 64-bit integers represented by using <tt class="docutils literal"><span class="pre">&lt;i8&gt;</span></tt> in XML-RPC responses
(contributed by Riku Lindblad; <a class="reference external" href="http://bugs.python.org/issue2985">issue 2985</a>).</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><tt class="xref py py-mod docutils literal"><span class="pre">zipfile</span></tt></a> module&#8217;s <tt class="xref py py-class docutils literal"><span class="pre">ZipFile</span></tt> class now has
<tt class="xref py py-meth docutils literal"><span class="pre">extract()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">extractall()</span></tt> methods that will unpack
a single file or all the files in the archive to the current directory, or
to a specified directory:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="n">z</span> <span class="o">=</span> <span class="n">zipfile</span><span class="o">.</span><span class="n">ZipFile</span><span class="p">(</span><span class="s">&#39;python-251.zip&#39;</span><span class="p">)</span>

<span class="c"># Unpack a single file, writing it relative</span>
<span class="c"># to the /tmp directory.</span>
<span class="n">z</span><span class="o">.</span><span class="n">extract</span><span class="p">(</span><span class="s">&#39;Python/sysmodule.c&#39;</span><span class="p">,</span> <span class="s">&#39;/tmp&#39;</span><span class="p">)</span>

<span class="c"># Unpack all the files in the archive.</span>
<span class="n">z</span><span class="o">.</span><span class="n">extractall</span><span class="p">()</span>
</pre></div>
</div>
<p>(Contributed by Alan McIntyre; <a class="reference external" href="http://bugs.python.org/issue467924">issue 467924</a>.)</p>
<p>The <a class="reference internal" href="../library/functions.html#open" title="open"><tt class="xref py py-meth docutils literal"><span class="pre">open()</span></tt></a>, <tt class="xref py py-meth docutils literal"><span class="pre">read()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">extract()</span></tt> methods can now
take either a filename or a <tt class="xref py py-class docutils literal"><span class="pre">ZipInfo</span></tt> object.  This is useful when an
archive accidentally contains a duplicated filename.
(Contributed by Graham Horler; <a class="reference external" href="http://bugs.python.org/issue1775025">issue 1775025</a>.)</p>
<p>Finally, <a class="reference internal" href="../library/zipfile.html#module-zipfile" title="zipfile: Read and write ZIP-format archive files."><tt class="xref py py-mod docutils literal"><span class="pre">zipfile</span></tt></a> now supports using Unicode filenames
for archived files.  (Contributed by Alexey Borzenkov; <a class="reference external" href="http://bugs.python.org/issue1734346">issue 1734346</a>.)</p>
</li>
</ul>
<div class="section" id="the-ast-module">
<h3>The <a class="reference internal" href="../library/ast.html#module-ast" title="ast: Abstract Syntax Tree classes and manipulation."><tt class="xref py py-mod docutils literal"><span class="pre">ast</span></tt></a> module<a class="headerlink" href="#the-ast-module" title="Permalink to this headline">¶</a></h3>
<p>The <a class="reference internal" href="../library/ast.html#module-ast" title="ast: Abstract Syntax Tree classes and manipulation."><tt class="xref py py-mod docutils literal"><span class="pre">ast</span></tt></a> module provides an Abstract Syntax Tree
representation of Python code, and Armin Ronacher
contributed a set of helper functions that perform a variety of
common tasks.  These will be useful for HTML templating
packages, code analyzers, and similar tools that process
Python code.</p>
<p>The <tt class="xref py py-func docutils literal"><span class="pre">parse()</span></tt> function takes an expression and returns an AST.
The <tt class="xref py py-func docutils literal"><span class="pre">dump()</span></tt> function outputs a representation of a tree, suitable
for debugging:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">ast</span>

<span class="n">t</span> <span class="o">=</span> <span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s">&quot;&quot;&quot;</span>
<span class="s">d = {}</span>
<span class="s">for i in &#39;abcdefghijklm&#39;:</span>
<span class="s">    d[i + i] = ord(i) - ord(&#39;a&#39;) + 1</span>
<span class="s">print d</span>
<span class="s">&quot;&quot;&quot;</span><span class="p">)</span>
<span class="k">print</span> <span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
</pre></div>
</div>
<p>This outputs a deeply nested tree:</p>
<div class="highlight-python"><pre>Module(body=[
  Assign(targets=[
    Name(id='d', ctx=Store())
   ], value=Dict(keys=[], values=[]))
  For(target=Name(id='i', ctx=Store()),
      iter=Str(s='abcdefghijklm'), body=[
    Assign(targets=[
      Subscript(value=
        Name(id='d', ctx=Load()),
          slice=
          Index(value=
            BinOp(left=Name(id='i', ctx=Load()), op=Add(),
             right=Name(id='i', ctx=Load()))), ctx=Store())
     ], value=
     BinOp(left=
      BinOp(left=
       Call(func=
        Name(id='ord', ctx=Load()), args=[
          Name(id='i', ctx=Load())
         ], keywords=[], starargs=None, kwargs=None),
       op=Sub(), right=Call(func=
        Name(id='ord', ctx=Load()), args=[
          Str(s='a')
         ], keywords=[], starargs=None, kwargs=None)),
       op=Add(), right=Num(n=1)))
    ], orelse=[])
   Print(dest=None, values=[
     Name(id='d', ctx=Load())
   ], nl=True)
 ])</pre>
</div>
<p>The <tt class="xref py py-func docutils literal"><span class="pre">literal_eval()</span></tt> method takes a string or an AST
representing a literal expression, parses and evaluates it, and
returns the resulting value.  A literal expression is a Python
expression containing only strings, numbers, dictionaries,
etc. but no statements or function calls.  If you need to
evaluate an expression but cannot accept the security risk of using an
<a class="reference internal" href="../library/functions.html#eval" title="eval"><tt class="xref py py-func docutils literal"><span class="pre">eval()</span></tt></a> call, <tt class="xref py py-func docutils literal"><span class="pre">literal_eval()</span></tt> will handle it safely:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="n">literal</span> <span class="o">=</span> <span class="s">&#39;(&quot;a&quot;, &quot;b&quot;, {2:4, 3:8, 1:2})&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">ast</span><span class="o">.</span><span class="n">literal_eval</span><span class="p">(</span><span class="n">literal</span><span class="p">)</span>
<span class="go">(&#39;a&#39;, &#39;b&#39;, {1: 2, 2: 4, 3: 8})</span>
<span class="gp">&gt;&gt;&gt; </span><span class="k">print</span> <span class="n">ast</span><span class="o">.</span><span class="n">literal_eval</span><span class="p">(</span><span class="s">&#39;&quot;a&quot; + &quot;b&quot;&#39;</span><span class="p">)</span>
<span class="gt">Traceback (most recent call last):</span>
  <span class="c">...</span>
<span class="nc">ValueError</span>: <span class="n-Identifier">malformed string</span>
</pre></div>
</div>
<p>The module also includes <tt class="xref py py-class docutils literal"><span class="pre">NodeVisitor</span></tt> and
<tt class="xref py py-class docutils literal"><span class="pre">NodeTransformer</span></tt> classes for traversing and modifying an AST,
and functions for common transformations such as changing line
numbers.</p>
</div>
<div class="section" id="the-future-builtins-module">
<h3>The <a class="reference internal" href="../library/future_builtins.html#module-future_builtins" title="future_builtins"><tt class="xref py py-mod docutils literal"><span class="pre">future_builtins</span></tt></a> module<a class="headerlink" href="#the-future-builtins-module" title="Permalink to this headline">¶</a></h3>
<p>Python 3.0 makes many changes to the repertoire of built-in
functions, and most of the changes can&#8217;t be introduced in the Python
2.x series because they would break compatibility.
The <a class="reference internal" href="../library/future_builtins.html#module-future_builtins" title="future_builtins"><tt class="xref py py-mod docutils literal"><span class="pre">future_builtins</span></tt></a> module provides versions
of these built-in functions that can be imported when writing
3.0-compatible code.</p>
<p>The functions in this module currently include:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">ascii(obj)</span></tt>: equivalent to <a class="reference internal" href="../library/repr.html#module-repr" title="repr: Alternate repr() implementation with size limits."><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a>.  In Python 3.0,
<a class="reference internal" href="../library/repr.html#module-repr" title="repr: Alternate repr() implementation with size limits."><tt class="xref py py-func docutils literal"><span class="pre">repr()</span></tt></a> will return a Unicode string, while <tt class="xref py py-func docutils literal"><span class="pre">ascii()</span></tt> will
return a pure ASCII bytestring.</li>
<li><tt class="docutils literal"><span class="pre">filter(predicate,</span> <span class="pre">iterable)</span></tt>,
<tt class="docutils literal"><span class="pre">map(func,</span> <span class="pre">iterable1,</span> <span class="pre">...)</span></tt>: the 3.0 versions
return iterators, unlike the 2.x builtins which return lists.</li>
<li><tt class="docutils literal"><span class="pre">hex(value)</span></tt>, <tt class="docutils literal"><span class="pre">oct(value)</span></tt>: instead of calling the
<a class="reference internal" href="../reference/datamodel.html#object.__hex__" title="object.__hex__"><tt class="xref py py-meth docutils literal"><span class="pre">__hex__()</span></tt></a> or <a class="reference internal" href="../reference/datamodel.html#object.__oct__" title="object.__oct__"><tt class="xref py py-meth docutils literal"><span class="pre">__oct__()</span></tt></a> methods, these versions will
call the <a class="reference internal" href="../reference/datamodel.html#object.__index__" title="object.__index__"><tt class="xref py py-meth docutils literal"><span class="pre">__index__()</span></tt></a> method and convert the result to hexadecimal
or octal.  <a class="reference internal" href="../library/functions.html#oct" title="oct"><tt class="xref py py-func docutils literal"><span class="pre">oct()</span></tt></a> will use the new <tt class="docutils literal"><span class="pre">0o</span></tt> notation for its
result.</li>
</ul>
</div>
<div class="section" id="the-json-module-javascript-object-notation">
<h3>The <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><tt class="xref py py-mod docutils literal"><span class="pre">json</span></tt></a> module: JavaScript Object Notation<a class="headerlink" href="#the-json-module-javascript-object-notation" title="Permalink to this headline">¶</a></h3>
<p>The new <a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><tt class="xref py py-mod docutils literal"><span class="pre">json</span></tt></a> module supports the encoding and decoding of Python types in
JSON (Javascript Object Notation). JSON is a lightweight interchange format
often used in web applications. For more information about JSON, see
<a class="reference external" href="http://www.json.org">http://www.json.org</a>.</p>
<p><a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><tt class="xref py py-mod docutils literal"><span class="pre">json</span></tt></a> comes with support for decoding and encoding most built-in Python
types. The following example encodes and decodes a dictionary:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="gp">&gt;&gt;&gt; </span><span class="kn">import</span> <span class="nn">json</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">data</span> <span class="o">=</span> <span class="p">{</span><span class="s">&quot;spam&quot;</span><span class="p">:</span> <span class="s">&quot;foo&quot;</span><span class="p">,</span> <span class="s">&quot;parrot&quot;</span><span class="p">:</span> <span class="mi">42</span><span class="p">}</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">in_json</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="n">dumps</span><span class="p">(</span><span class="n">data</span><span class="p">)</span> <span class="c"># Encode the data</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">in_json</span>
<span class="go">&#39;{&quot;parrot&quot;: 42, &quot;spam&quot;: &quot;foo&quot;}&#39;</span>
<span class="gp">&gt;&gt;&gt; </span><span class="n">json</span><span class="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">in_json</span><span class="p">)</span> <span class="c"># Decode into a Python object</span>
<span class="go">{&quot;spam&quot;: &quot;foo&quot;, &quot;parrot&quot;: 42}</span>
</pre></div>
</div>
<p>It&#8217;s also possible to write your own decoders and encoders to support
more types. Pretty-printing of the JSON strings is also supported.</p>
<p><a class="reference internal" href="../library/json.html#module-json" title="json: Encode and decode the JSON format."><tt class="xref py py-mod docutils literal"><span class="pre">json</span></tt></a> (originally called simplejson) was written by Bob
Ippolito.</p>
</div>
<div class="section" id="the-plistlib-module-a-property-list-parser">
<h3>The <a class="reference internal" href="../library/plistlib.html#module-plistlib" title="plistlib: Generate and parse Mac OS X plist files."><tt class="xref py py-mod docutils literal"><span class="pre">plistlib</span></tt></a> module: A Property-List Parser<a class="headerlink" href="#the-plistlib-module-a-property-list-parser" title="Permalink to this headline">¶</a></h3>
<p>The <tt class="docutils literal"><span class="pre">.plist</span></tt> format is commonly used on Mac OS X to
store basic data types (numbers, strings, lists,
and dictionaries) by serializing them into an XML-based format.
It resembles the XML-RPC serialization of data types.</p>
<p>Despite being primarily used on Mac OS X, the format
has nothing Mac-specific about it and the Python implementation works
on any platform that Python supports, so the <a class="reference internal" href="../library/plistlib.html#module-plistlib" title="plistlib: Generate and parse Mac OS X plist files."><tt class="xref py py-mod docutils literal"><span class="pre">plistlib</span></tt></a> module
has been promoted to the standard library.</p>
<p>Using the module is simple:</p>
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">plistlib</span>
<span class="kn">import</span> <span class="nn">datetime</span>

<span class="c"># Create data structure</span>
<span class="n">data_struct</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">(</span><span class="n">lastAccessed</span><span class="o">=</span><span class="n">datetime</span><span class="o">.</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">(),</span>
                   <span class="n">version</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span>
                   <span class="n">categories</span><span class="o">=</span><span class="p">(</span><span class="s">&#39;Personal&#39;</span><span class="p">,</span><span class="s">&#39;Shared&#39;</span><span class="p">,</span><span class="s">&#39;Private&#39;</span><span class="p">))</span>

<span class="c"># Create string containing XML.</span>
<span class="n">plist_str</span> <span class="o">=</span> <span class="n">plistlib</span><span class="o">.</span><span class="n">writePlistToString</span><span class="p">(</span><span class="n">data_struct</span><span class="p">)</span>
<span class="n">new_struct</span> <span class="o">=</span> <span class="n">plistlib</span><span class="o">.</span><span class="n">readPlistFromString</span><span class="p">(</span><span class="n">plist_str</span><span class="p">)</span>
<span class="k">print</span> <span class="n">data_struct</span>
<span class="k">print</span> <span class="n">new_struct</span>

<span class="c"># Write data structure to a file and read it back.</span>
<span class="n">plistlib</span><span class="o">.</span><span class="n">writePlist</span><span class="p">(</span><span class="n">data_struct</span><span class="p">,</span> <span class="s">&#39;/tmp/customizations.plist&#39;</span><span class="p">)</span>
<span class="n">new_struct</span> <span class="o">=</span> <span class="n">plistlib</span><span class="o">.</span><span class="n">readPlist</span><span class="p">(</span><span class="s">&#39;/tmp/customizations.plist&#39;</span><span class="p">)</span>

<span class="c"># read/writePlist accepts file-like objects as well as paths.</span>
<span class="n">plistlib</span><span class="o">.</span><span class="n">writePlist</span><span class="p">(</span><span class="n">data_struct</span><span class="p">,</span> <span class="n">sys</span><span class="o">.</span><span class="n">stdout</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="ctypes-enhancements">
<h3>ctypes Enhancements<a class="headerlink" href="#ctypes-enhancements" title="Permalink to this headline">¶</a></h3>
<p>Thomas Heller continued to maintain and enhance the
<a class="reference internal" href="../library/ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><tt class="xref py py-mod docutils literal"><span class="pre">ctypes</span></tt></a> module.</p>
<p><a class="reference internal" href="../library/ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><tt class="xref py py-mod docutils literal"><span class="pre">ctypes</span></tt></a> now supports a <tt class="xref py py-class docutils literal"><span class="pre">c_bool</span></tt> datatype
that represents the C99 <tt class="docutils literal"><span class="pre">bool</span></tt> type.  (Contributed by David Remahl;
<a class="reference external" href="http://bugs.python.org/issue1649190">issue 1649190</a>.)</p>
<p>The <a class="reference internal" href="../library/ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><tt class="xref py py-mod docutils literal"><span class="pre">ctypes</span></tt></a> string, buffer and array types have improved
support for extended slicing syntax,
where various combinations of <tt class="docutils literal"><span class="pre">(start,</span> <span class="pre">stop,</span> <span class="pre">step)</span></tt> are supplied.
(Implemented by Thomas Wouters.)</p>
<p>All <a class="reference internal" href="../library/ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><tt class="xref py py-mod docutils literal"><span class="pre">ctypes</span></tt></a> data types now support
<tt class="xref py py-meth docutils literal"><span class="pre">from_buffer()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">from_buffer_copy()</span></tt>
methods that create a ctypes instance based on a
provided buffer object.  <tt class="xref py py-meth docutils literal"><span class="pre">from_buffer_copy()</span></tt> copies
the contents of the object,
while <tt class="xref py py-meth docutils literal"><span class="pre">from_buffer()</span></tt> will share the same memory area.</p>
<p>A new calling convention tells <a class="reference internal" href="../library/ctypes.html#module-ctypes" title="ctypes: A foreign function library for Python."><tt class="xref py py-mod docutils literal"><span class="pre">ctypes</span></tt></a> to clear the <tt class="docutils literal"><span class="pre">errno</span></tt> or
Win32 LastError variables at the outset of each wrapped call.
(Implemented by Thomas Heller; <a class="reference external" href="http://bugs.python.org/issue1798">issue 1798</a>.)</p>
<p>You can now retrieve the Unix <tt class="docutils literal"><span class="pre">errno</span></tt> variable after a function
call.  When creating a wrapped function, you can supply
<tt class="docutils literal"><span class="pre">use_errno=True</span></tt> as a keyword parameter to the <tt class="xref py py-func docutils literal"><span class="pre">DLL()</span></tt> function
and then call the module-level methods <tt class="xref py py-meth docutils literal"><span class="pre">set_errno()</span></tt> and
<tt class="xref py py-meth docutils literal"><span class="pre">get_errno()</span></tt> to set and retrieve the error value.</p>
<p>The Win32 LastError variable is similarly supported by
the <tt class="xref py py-func docutils literal"><span class="pre">DLL()</span></tt>, <tt class="xref py py-func docutils literal"><span class="pre">OleDLL()</span></tt>, and <tt class="xref py py-func docutils literal"><span class="pre">WinDLL()</span></tt> functions.
You supply <tt class="docutils literal"><span class="pre">use_last_error=True</span></tt> as a keyword parameter
and then call the module-level methods <tt class="xref py py-meth docutils literal"><span class="pre">set_last_error()</span></tt>
and <tt class="xref py py-meth docutils literal"><span class="pre">get_last_error()</span></tt>.</p>
<p>The <tt class="xref py py-func docutils literal"><span class="pre">byref()</span></tt> function, used to retrieve a pointer to a ctypes
instance, now has an optional <em>offset</em> parameter that is a byte
count that will be added to the returned pointer.</p>
</div>
<div class="section" id="improved-ssl-support">
<h3>Improved SSL Support<a class="headerlink" href="#improved-ssl-support" title="Permalink to this headline">¶</a></h3>
<p>Bill Janssen made extensive improvements to Python 2.6&#8217;s support for
the Secure Sockets Layer by adding a new module, <a class="reference internal" href="../library/ssl.html#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><tt class="xref py py-mod docutils literal"><span class="pre">ssl</span></tt></a>, that&#8217;s
built atop the <a class="reference external" href="http://www.openssl.org/">OpenSSL</a> library.
This new module provides more control over the protocol negotiated,
the X.509 certificates used, and has better support for writing SSL
servers (as opposed to clients) in Python.  The existing SSL support
in the <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><tt class="xref py py-mod docutils literal"><span class="pre">socket</span></tt></a> module hasn&#8217;t been removed and continues to work,
though it will be removed in Python 3.0.</p>
<p>To use the new module, you must first create a TCP connection in the
usual way and then pass it to the <a class="reference internal" href="../library/ssl.html#ssl.wrap_socket" title="ssl.wrap_socket"><tt class="xref py py-func docutils literal"><span class="pre">ssl.wrap_socket()</span></tt></a> function.
It&#8217;s possible to specify whether a certificate is required, and to
obtain certificate info by calling the <tt class="xref py py-meth docutils literal"><span class="pre">getpeercert()</span></tt> method.</p>
<div class="admonition-see-also admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">The documentation for the <a class="reference internal" href="../library/ssl.html#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><tt class="xref py py-mod docutils literal"><span class="pre">ssl</span></tt></a> module.</p>
</div>
</div>
</div>
<div class="section" id="deprecations-and-removals">
<h2>Deprecations and Removals<a class="headerlink" href="#deprecations-and-removals" title="Permalink to this headline">¶</a></h2>
<ul>
<li><p class="first">String exceptions have been removed.  Attempting to use them raises a
<a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a>.</p>
</li>
<li><p class="first">Changes to the <tt class="xref py py-class docutils literal"><span class="pre">Exception</span></tt> interface
as dictated by <span class="target" id="index-25"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0352"><strong>PEP 352</strong></a> continue to be made.  For 2.6,
the <tt class="xref py py-attr docutils literal"><span class="pre">message</span></tt> attribute is being deprecated in favor of the
<tt class="xref py py-attr docutils literal"><span class="pre">args</span></tt> attribute.</p>
</li>
<li><p class="first">(3.0-warning mode) Python 3.0 will feature a reorganized standard
library that will drop many outdated modules and rename others.
Python 2.6 running in 3.0-warning mode will warn about these modules
when they are imported.</p>
<p>The list of deprecated modules is:
<tt class="xref py py-mod docutils literal"><span class="pre">audiodev</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">bgenlocations</span></tt>,
<a class="reference internal" href="../library/undoc.html#module-buildtools" title="buildtools: Helper module for BuildApplet, BuildApplication and macfreeze. (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">buildtools</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">bundlebuilder</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">Canvas</span></tt>,
<a class="reference internal" href="../library/compiler.html#module-compiler" title="compiler: Python code compiler written in Python. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">compiler</span></tt></a>,
<a class="reference internal" href="../library/dircache.html#module-dircache" title="dircache: Return directory listing, with cache mechanism. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">dircache</span></tt></a>,
<a class="reference internal" href="../library/dl.html#module-dl" title="dl: Call C functions in shared objects. (deprecated) (Unix)"><tt class="xref py py-mod docutils literal"><span class="pre">dl</span></tt></a>,
<a class="reference internal" href="../library/fpformat.html#module-fpformat" title="fpformat: General floating point formatting functions. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">fpformat</span></tt></a>,
<a class="reference internal" href="../library/gensuitemodule.html#module-gensuitemodule" title="gensuitemodule: Create a stub package from an OSA dictionary (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">gensuitemodule</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">ihooks</span></tt>,
<a class="reference internal" href="../library/imageop.html#module-imageop" title="imageop: Manipulate raw image data. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">imageop</span></tt></a>,
<a class="reference internal" href="../library/imgfile.html#module-imgfile" title="imgfile: Support for SGI imglib files. (deprecated) (IRIX)"><tt class="xref py py-mod docutils literal"><span class="pre">imgfile</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">linuxaudiodev</span></tt>,
<a class="reference internal" href="../library/mhlib.html#module-mhlib" title="mhlib: Manipulate MH mailboxes from Python. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">mhlib</span></tt></a>,
<a class="reference internal" href="../library/mimetools.html#module-mimetools" title="mimetools: Tools for parsing MIME-style message bodies. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">mimetools</span></tt></a>,
<a class="reference internal" href="../library/multifile.html#module-multifile" title="multifile: Support for reading files which contain distinct parts, such as some MIME data. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">multifile</span></tt></a>,
<a class="reference internal" href="../library/new.html#module-new" title="new: Interface to the creation of runtime implementation objects. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">new</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">pure</span></tt>,
<a class="reference internal" href="../library/statvfs.html#module-statvfs" title="statvfs: Constants for interpreting the result of os.statvfs(). (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">statvfs</span></tt></a>,
<a class="reference internal" href="../library/sunaudio.html#module-sunaudiodev" title="sunaudiodev: Access to Sun audio hardware. (deprecated) (SunOS)"><tt class="xref py py-mod docutils literal"><span class="pre">sunaudiodev</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">test.testall</span></tt>, and
<tt class="xref py py-mod docutils literal"><span class="pre">toaiff</span></tt>.</p>
</li>
<li><p class="first">The <tt class="xref py py-mod docutils literal"><span class="pre">gopherlib</span></tt> module has been removed.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/mimewriter.html#module-MimeWriter" title="MimeWriter: Write MIME format files. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">MimeWriter</span></tt></a> module and <a class="reference internal" href="../library/mimify.html#module-mimify" title="mimify: Mimification and unmimification of mail messages. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">mimify</span></tt></a> module
have been deprecated; use the <a class="reference internal" href="../library/email.html#module-email" title="email: Package supporting the parsing, manipulating, and generating email messages, including MIME documents."><tt class="xref py py-mod docutils literal"><span class="pre">email</span></tt></a>
package instead.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/md5.html#module-md5" title="md5: RSA's MD5 message digest algorithm. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">md5</span></tt></a> module has been deprecated; use the <a class="reference internal" href="../library/hashlib.html#module-hashlib" title="hashlib: Secure hash and message digest algorithms."><tt class="xref py py-mod docutils literal"><span class="pre">hashlib</span></tt></a> module
instead.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/posixfile.html#module-posixfile" title="posixfile: A file-like object with support for locking. (deprecated) (Unix)"><tt class="xref py py-mod docutils literal"><span class="pre">posixfile</span></tt></a> module has been deprecated; <a class="reference internal" href="../library/fcntl.html#fcntl.lockf" title="fcntl.lockf"><tt class="xref py py-func docutils literal"><span class="pre">fcntl.lockf()</span></tt></a>
provides better locking.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/popen2.html#module-popen2" title="popen2: Subprocesses with accessible standard I/O streams. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">popen2</span></tt></a> module has been deprecated; use the <a class="reference internal" href="../library/subprocess.html#module-subprocess" title="subprocess: Subprocess management."><tt class="xref py py-mod docutils literal"><span class="pre">subprocess</span></tt></a>
module.</p>
</li>
<li><p class="first">The <tt class="xref py py-mod docutils literal"><span class="pre">rgbimg</span></tt> module has been removed.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/sets.html#module-sets" title="sets: Implementation of sets of unique elements. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">sets</span></tt></a> module has been deprecated; it&#8217;s better to
use the built-in <a class="reference internal" href="../library/stdtypes.html#set" title="set"><tt class="xref py py-class docutils literal"><span class="pre">set</span></tt></a> and <a class="reference internal" href="../library/stdtypes.html#frozenset" title="frozenset"><tt class="xref py py-class docutils literal"><span class="pre">frozenset</span></tt></a> types.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/sha.html#module-sha" title="sha: NIST's secure hash algorithm, SHA. (deprecated)"><tt class="xref py py-mod docutils literal"><span class="pre">sha</span></tt></a> module has been deprecated; use the <a class="reference internal" href="../library/hashlib.html#module-hashlib" title="hashlib: Secure hash and message digest algorithms."><tt class="xref py py-mod docutils literal"><span class="pre">hashlib</span></tt></a> module
instead.</p>
</li>
</ul>
</div>
<div class="section" id="build-and-c-api-changes">
<h2>Build and C API Changes<a class="headerlink" href="#build-and-c-api-changes" title="Permalink to this headline">¶</a></h2>
<p>Changes to Python&#8217;s build process and to the C API include:</p>
<ul>
<li><p class="first">Python now must be compiled with C89 compilers (after 19
years!).  This means that the Python source tree has dropped its
own implementations of <tt class="xref c c-func docutils literal"><span class="pre">memmove()</span></tt> and <tt class="xref c c-func docutils literal"><span class="pre">strerror()</span></tt>, which
are in the C89 standard library.</p>
</li>
<li><p class="first">Python 2.6 can be built with Microsoft Visual Studio 2008 (version
9.0), and this is the new default compiler.  See the
<tt class="file docutils literal"><span class="pre">PCbuild</span></tt> directory for the build files.  (Implemented by
Christian Heimes.)</p>
</li>
<li><p class="first">On Mac OS X, Python 2.6 can be compiled as a 4-way universal build.
The <strong class="program">configure</strong> script
can take a <em class="xref std std-option">--with-universal-archs=[32-bit|64-bit|all]</em>
switch, controlling whether the binaries are built for 32-bit
architectures (x86, PowerPC), 64-bit (x86-64 and PPC-64), or both.
(Contributed by Ronald Oussoren.)</p>
</li>
<li><p class="first">The BerkeleyDB module now has a C API object, available as
<tt class="docutils literal"><span class="pre">bsddb.db.api</span></tt>.   This object can be used by other C extensions
that wish to use the <a class="reference internal" href="../library/bsddb.html#module-bsddb" title="bsddb: Interface to Berkeley DB database library"><tt class="xref py py-mod docutils literal"><span class="pre">bsddb</span></tt></a> module for their own purposes.
(Contributed by Duncan Grisby.)</p>
</li>
<li><p class="first">The new buffer interface, previously described in
<a class="reference external" href="#pep-3118-revised-buffer-protocol">the PEP 3118 section</a>,
adds <a class="reference internal" href="../c-api/buffer.html#PyObject_GetBuffer" title="PyObject_GetBuffer"><tt class="xref c c-func docutils literal"><span class="pre">PyObject_GetBuffer()</span></tt></a> and <a class="reference internal" href="../c-api/buffer.html#PyBuffer_Release" title="PyBuffer_Release"><tt class="xref c c-func docutils literal"><span class="pre">PyBuffer_Release()</span></tt></a>,
as well as a few other functions.</p>
</li>
<li><p class="first">Python&#8217;s use of the C stdio library is now thread-safe, or at least
as thread-safe as the underlying library is.  A long-standing potential
bug occurred if one thread closed a file object while another thread
was reading from or writing to the object.  In 2.6 file objects
have a reference count, manipulated by the
<a class="reference internal" href="../c-api/file.html#PyFile_IncUseCount" title="PyFile_IncUseCount"><tt class="xref c c-func docutils literal"><span class="pre">PyFile_IncUseCount()</span></tt></a> and <a class="reference internal" href="../c-api/file.html#PyFile_DecUseCount" title="PyFile_DecUseCount"><tt class="xref c c-func docutils literal"><span class="pre">PyFile_DecUseCount()</span></tt></a>
functions.  File objects can&#8217;t be closed unless the reference count
is zero.  <a class="reference internal" href="../c-api/file.html#PyFile_IncUseCount" title="PyFile_IncUseCount"><tt class="xref c c-func docutils literal"><span class="pre">PyFile_IncUseCount()</span></tt></a> should be called while the GIL
is still held, before carrying out an I/O operation using the
<tt class="docutils literal"><span class="pre">FILE</span> <span class="pre">*</span></tt> pointer, and <a class="reference internal" href="../c-api/file.html#PyFile_DecUseCount" title="PyFile_DecUseCount"><tt class="xref c c-func docutils literal"><span class="pre">PyFile_DecUseCount()</span></tt></a> should be called
immediately after the GIL is re-acquired.
(Contributed by Antoine Pitrou and Gregory P. Smith.)</p>
</li>
<li><p class="first">Importing modules simultaneously in two different threads no longer
deadlocks; it will now raise an <a class="reference internal" href="../library/exceptions.html#exceptions.ImportError" title="exceptions.ImportError"><tt class="xref py py-exc docutils literal"><span class="pre">ImportError</span></tt></a>.  A new API
function, <a class="reference internal" href="../c-api/import.html#PyImport_ImportModuleNoBlock" title="PyImport_ImportModuleNoBlock"><tt class="xref c c-func docutils literal"><span class="pre">PyImport_ImportModuleNoBlock()</span></tt></a>, will look for a
module in <tt class="docutils literal"><span class="pre">sys.modules</span></tt> first, then try to import it after
acquiring an import lock.  If the import lock is held by another
thread, an <a class="reference internal" href="../library/exceptions.html#exceptions.ImportError" title="exceptions.ImportError"><tt class="xref py py-exc docutils literal"><span class="pre">ImportError</span></tt></a> is raised.
(Contributed by Christian Heimes.)</p>
</li>
<li><p class="first">Several functions return information about the platform&#8217;s
floating-point support.  <a class="reference internal" href="../c-api/float.html#PyFloat_GetMax" title="PyFloat_GetMax"><tt class="xref c c-func docutils literal"><span class="pre">PyFloat_GetMax()</span></tt></a> returns
the maximum representable floating point value,
and <a class="reference internal" href="../c-api/float.html#PyFloat_GetMin" title="PyFloat_GetMin"><tt class="xref c c-func docutils literal"><span class="pre">PyFloat_GetMin()</span></tt></a> returns the minimum
positive value.  <a class="reference internal" href="../c-api/float.html#PyFloat_GetInfo" title="PyFloat_GetInfo"><tt class="xref c c-func docutils literal"><span class="pre">PyFloat_GetInfo()</span></tt></a> returns an object
containing more information from the <tt class="file docutils literal"><span class="pre">float.h</span></tt> file, such as
<tt class="docutils literal"><span class="pre">&quot;mant_dig&quot;</span></tt> (number of digits in the mantissa), <tt class="docutils literal"><span class="pre">&quot;epsilon&quot;</span></tt>
(smallest difference between 1.0 and the next largest value
representable), and several others.
(Contributed by Christian Heimes; <a class="reference external" href="http://bugs.python.org/issue1534">issue 1534</a>.)</p>
</li>
<li><p class="first">C functions and methods that use
<a class="reference internal" href="../c-api/complex.html#PyComplex_AsCComplex" title="PyComplex_AsCComplex"><tt class="xref c c-func docutils literal"><span class="pre">PyComplex_AsCComplex()</span></tt></a> will now accept arguments that
have a <a class="reference internal" href="../reference/datamodel.html#object.__complex__" title="object.__complex__"><tt class="xref py py-meth docutils literal"><span class="pre">__complex__()</span></tt></a> method.  In particular, the functions in the
<a class="reference internal" href="../library/cmath.html#module-cmath" title="cmath: Mathematical functions for complex numbers."><tt class="xref py py-mod docutils literal"><span class="pre">cmath</span></tt></a> module will now accept objects with this method.
This is a backport of a Python 3.0 change.
(Contributed by Mark Dickinson; <a class="reference external" href="http://bugs.python.org/issue1675423">issue 1675423</a>.)</p>
</li>
<li><p class="first">Python&#8217;s C API now includes two functions for case-insensitive string
comparisons, <tt class="docutils literal"><span class="pre">PyOS_stricmp(char*,</span> <span class="pre">char*)</span></tt>
and <tt class="docutils literal"><span class="pre">PyOS_strnicmp(char*,</span> <span class="pre">char*,</span> <span class="pre">Py_ssize_t)</span></tt>.
(Contributed by Christian Heimes; <a class="reference external" href="http://bugs.python.org/issue1635">issue 1635</a>.)</p>
</li>
<li><p class="first">Many C extensions define their own little macro for adding
integers and strings to the module&#8217;s dictionary in the
<tt class="docutils literal"><span class="pre">init*</span></tt> function.  Python 2.6 finally defines standard macros
for adding values to a module, <a class="reference internal" href="../c-api/module.html#PyModule_AddStringMacro" title="PyModule_AddStringMacro"><tt class="xref c c-macro docutils literal"><span class="pre">PyModule_AddStringMacro</span></tt></a>
and <tt class="xref c c-macro docutils literal"><span class="pre">PyModule_AddIntMacro()</span></tt>.  (Contributed by
Christian Heimes.)</p>
</li>
<li><p class="first">Some macros were renamed in both 3.0 and 2.6 to make it clearer that
they are macros,
not functions.  <tt class="xref c c-macro docutils literal"><span class="pre">Py_Size()</span></tt> became <tt class="xref c c-macro docutils literal"><span class="pre">Py_SIZE()</span></tt>,
<tt class="xref c c-macro docutils literal"><span class="pre">Py_Type()</span></tt> became <tt class="xref c c-macro docutils literal"><span class="pre">Py_TYPE()</span></tt>, and
<tt class="xref c c-macro docutils literal"><span class="pre">Py_Refcnt()</span></tt> became <tt class="xref c c-macro docutils literal"><span class="pre">Py_REFCNT()</span></tt>.
The mixed-case macros are still available
in Python 2.6 for backward compatibility.
(<a class="reference external" href="http://bugs.python.org/issue1629">issue 1629</a>)</p>
</li>
<li><p class="first">Distutils now places C extensions it builds in a
different directory when running on a debug version of Python.
(Contributed by Collin Winter; <a class="reference external" href="http://bugs.python.org/issue1530959">issue 1530959</a>.)</p>
</li>
<li><p class="first">Several basic data types, such as integers and strings, maintain
internal free lists of objects that can be re-used.  The data
structures for these free lists now follow a naming convention: the
variable is always named <tt class="docutils literal"><span class="pre">free_list</span></tt>, the counter is always named
<tt class="docutils literal"><span class="pre">numfree</span></tt>, and a macro <tt class="docutils literal"><span class="pre">Py&lt;typename&gt;_MAXFREELIST</span></tt> is
always defined.</p>
</li>
<li><p class="first">A new Makefile target, &#8220;make patchcheck&#8221;, prepares the Python source tree
for making a patch: it fixes trailing whitespace in all modified
<tt class="docutils literal"><span class="pre">.py</span></tt> files, checks whether the documentation has been changed,
and reports whether the <tt class="file docutils literal"><span class="pre">Misc/ACKS</span></tt> and <tt class="file docutils literal"><span class="pre">Misc/NEWS</span></tt> files
have been updated.
(Contributed by Brett Cannon.)</p>
<p>Another new target, &#8220;make profile-opt&#8221;, compiles a Python binary
using GCC&#8217;s profile-guided optimization.  It compiles Python with
profiling enabled, runs the test suite to obtain a set of profiling
results, and then compiles using these results for optimization.
(Contributed by Gregory P. Smith.)</p>
</li>
</ul>
<div class="section" id="port-specific-changes-windows">
<h3>Port-Specific Changes: Windows<a class="headerlink" href="#port-specific-changes-windows" title="Permalink to this headline">¶</a></h3>
<ul>
<li><p class="first">The support for Windows 95, 98, ME and NT4 has been dropped.
Python 2.6 requires at least Windows 2000 SP4.</p>
</li>
<li><p class="first">The new default compiler on Windows is Visual Studio 2008 (version
9.0). The build directories for Visual Studio 2003 (version 7.1) and
2005 (version 8.0) were moved into the PC/ directory. The new
<tt class="file docutils literal"><span class="pre">PCbuild</span></tt> directory supports cross compilation for X64, debug
builds and Profile Guided Optimization (PGO). PGO builds are roughly
10% faster than normal builds.  (Contributed by Christian Heimes
with help from Amaury Forgeot d&#8217;Arc and Martin von Loewis.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/msvcrt.html#module-msvcrt" title="msvcrt: Miscellaneous useful routines from the MS VC++ runtime. (Windows)"><tt class="xref py py-mod docutils literal"><span class="pre">msvcrt</span></tt></a> module now supports
both the normal and wide char variants of the console I/O
API.  The <tt class="xref py py-func docutils literal"><span class="pre">getwch()</span></tt> function reads a keypress and returns a Unicode
value, as does the <tt class="xref py py-func docutils literal"><span class="pre">getwche()</span></tt> function.  The <tt class="xref py py-func docutils literal"><span class="pre">putwch()</span></tt> function
takes a Unicode character and writes it to the console.
(Contributed by Christian Heimes.)</p>
</li>
<li><p class="first"><a class="reference internal" href="../library/os.path.html#os.path.expandvars" title="os.path.expandvars"><tt class="xref py py-func docutils literal"><span class="pre">os.path.expandvars()</span></tt></a> will now expand environment variables in
the form &#8220;%var%&#8221;, and &#8220;~user&#8221; will be expanded into the user&#8217;s home
directory path.  (Contributed by Josiah Carlson; <a class="reference external" href="http://bugs.python.org/issue957650">issue 957650</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><tt class="xref py py-mod docutils literal"><span class="pre">socket</span></tt></a> module&#8217;s socket objects now have an
<tt class="xref py py-meth docutils literal"><span class="pre">ioctl()</span></tt> method that provides a limited interface to the
<tt class="xref c c-func docutils literal"><span class="pre">WSAIoctl()</span></tt> system interface.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/_winreg.html#module-_winreg" title="_winreg: Routines and objects for manipulating the Windows registry. (Windows)"><tt class="xref py py-mod docutils literal"><span class="pre">_winreg</span></tt></a> module now has a function,
<tt class="xref py py-func docutils literal"><span class="pre">ExpandEnvironmentStrings()</span></tt>,
that expands environment variable references such as <tt class="docutils literal"><span class="pre">%NAME%</span></tt>
in an input string.  The handle objects provided by this
module now support the context protocol, so they can be used
in <a class="reference internal" href="../reference/compound_stmts.html#with"><tt class="xref std std-keyword docutils literal"><span class="pre">with</span></tt></a> statements. (Contributed by Christian Heimes.)</p>
<p><a class="reference internal" href="../library/_winreg.html#module-_winreg" title="_winreg: Routines and objects for manipulating the Windows registry. (Windows)"><tt class="xref py py-mod docutils literal"><span class="pre">_winreg</span></tt></a> also has better support for x64 systems,
exposing the <tt class="xref py py-func docutils literal"><span class="pre">DisableReflectionKey()</span></tt>, <tt class="xref py py-func docutils literal"><span class="pre">EnableReflectionKey()</span></tt>,
and <tt class="xref py py-func docutils literal"><span class="pre">QueryReflectionKey()</span></tt> functions, which enable and disable
registry reflection for 32-bit processes running on 64-bit systems.
(<a class="reference external" href="http://bugs.python.org/issue1753245">issue 1753245</a>)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/msilib.html#module-msilib" title="msilib: Creation of Microsoft Installer files, and CAB files. (Windows)"><tt class="xref py py-mod docutils literal"><span class="pre">msilib</span></tt></a> module&#8217;s <tt class="xref py py-class docutils literal"><span class="pre">Record</span></tt> object
gained <tt class="xref py py-meth docutils literal"><span class="pre">GetInteger()</span></tt> and <tt class="xref py py-meth docutils literal"><span class="pre">GetString()</span></tt> methods that
return field values as an integer or a string.
(Contributed by Floris Bruynooghe; <a class="reference external" href="http://bugs.python.org/issue2125">issue 2125</a>.)</p>
</li>
</ul>
</div>
<div class="section" id="port-specific-changes-mac-os-x">
<h3>Port-Specific Changes: Mac OS X<a class="headerlink" href="#port-specific-changes-mac-os-x" title="Permalink to this headline">¶</a></h3>
<ul class="simple">
<li>When compiling a framework build of Python, you can now specify the
framework name to be used by providing the
<em class="xref std std-option">--with-framework-name=</em> option to the
<strong class="program">configure</strong> script.</li>
<li>The <tt class="xref py py-mod docutils literal"><span class="pre">macfs</span></tt> module has been removed.  This in turn required the
<a class="reference internal" href="../library/macostools.html#macostools.touched" title="macostools.touched"><tt class="xref py py-func docutils literal"><span class="pre">macostools.touched()</span></tt></a> function to be removed because it depended on the
<tt class="xref py py-mod docutils literal"><span class="pre">macfs</span></tt> module.  (<a class="reference external" href="http://bugs.python.org/issue1490190">issue 1490190</a>)</li>
<li>Many other Mac OS modules have been deprecated and will removed in
Python 3.0:
<tt class="xref py py-mod docutils literal"><span class="pre">_builtinSuites</span></tt>,
<a class="reference internal" href="../library/aepack.html#module-aepack" title="aepack: Conversion between Python variables and AppleEvent data containers. (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">aepack</span></tt></a>,
<a class="reference internal" href="../library/aetools.html#module-aetools" title="aetools: Basic support for sending Apple Events (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">aetools</span></tt></a>,
<a class="reference internal" href="../library/aetypes.html#module-aetypes" title="aetypes: Python representation of the Apple Event Object Model. (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">aetypes</span></tt></a>,
<a class="reference internal" href="../library/undoc.html#module-applesingle" title="applesingle: Rudimentary decoder for AppleSingle format files. (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">applesingle</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">appletrawmain</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">appletrunner</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">argvemulator</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">Audio_mac</span></tt>,
<a class="reference internal" href="../library/autogil.html#module-autoGIL" title="autoGIL: Global Interpreter Lock handling in event loops. (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">autoGIL</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">Carbon</span></tt>,
<a class="reference internal" href="../library/undoc.html#module-cfmfile" title="cfmfile: Code Fragment Resource module. (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">cfmfile</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">CodeWarrior</span></tt>,
<a class="reference internal" href="../library/colorpicker.html#module-ColorPicker" title="ColorPicker: Interface to the standard color selection dialog. (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">ColorPicker</span></tt></a>,
<a class="reference internal" href="../library/easydialogs.html#module-EasyDialogs" title="EasyDialogs: Basic Macintosh dialogs. (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">EasyDialogs</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">Explorer</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">Finder</span></tt>,
<a class="reference internal" href="../library/framework.html#module-FrameWork" title="FrameWork: Interactive application framework. (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">FrameWork</span></tt></a>,
<a class="reference internal" href="../library/macostools.html#module-findertools" title="findertools: Wrappers around the finder's Apple Events interface. (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">findertools</span></tt></a>,
<a class="reference internal" href="../library/ic.html#module-ic" title="ic: Access to the Mac OS X Internet Config. (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">ic</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">icglue</span></tt>,
<a class="reference internal" href="../library/undoc.html#module-icopen" title="icopen: Internet Config replacement for open(). (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">icopen</span></tt></a>,
<a class="reference internal" href="../library/undoc.html#module-macerrors" title="macerrors: Constant definitions for many Mac OS error codes. (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">macerrors</span></tt></a>,
<a class="reference internal" href="../library/macos.html#module-MacOS" title="MacOS: Access to Mac OS-specific interpreter features. (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">MacOS</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">macfs</span></tt>,
<a class="reference internal" href="../library/macostools.html#module-macostools" title="macostools: Convenience routines for file manipulation. (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">macostools</span></tt></a>,
<a class="reference internal" href="../library/undoc.html#module-macresource" title="macresource: Locate script resources. (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">macresource</span></tt></a>,
<a class="reference internal" href="../library/miniaeframe.html#module-MiniAEFrame" title="MiniAEFrame: Support to act as an Open Scripting Architecture (OSA) server (&quot;Apple Events&quot;). (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">MiniAEFrame</span></tt></a>,
<a class="reference internal" href="../library/undoc.html#module-Nav" title="Nav: Interface to Navigation Services. (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">Nav</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">Netscape</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">OSATerminology</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">pimp</span></tt>,
<a class="reference internal" href="../library/undoc.html#module-PixMapWrapper" title="PixMapWrapper: Wrapper for PixMap objects. (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">PixMapWrapper</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">StdSuites</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">SystemEvents</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">Terminal</span></tt>, and
<tt class="xref py py-mod docutils literal"><span class="pre">terminalcommand</span></tt>.</li>
</ul>
</div>
<div class="section" id="port-specific-changes-irix">
<h3>Port-Specific Changes: IRIX<a class="headerlink" href="#port-specific-changes-irix" title="Permalink to this headline">¶</a></h3>
<p>A number of old IRIX-specific modules were deprecated and will
be removed in Python 3.0:
<a class="reference internal" href="../library/al.html#module-al" title="al: Audio functions on the SGI. (deprecated) (IRIX)"><tt class="xref py py-mod docutils literal"><span class="pre">al</span></tt></a> and <a class="reference internal" href="../library/al.html#module-AL" title="AL: Constants used with the al module. (deprecated) (IRIX)"><tt class="xref py py-mod docutils literal"><span class="pre">AL</span></tt></a>,
<a class="reference internal" href="../library/cd.html#module-cd" title="cd: Interface to the CD-ROM on Silicon Graphics systems. (deprecated) (IRIX)"><tt class="xref py py-mod docutils literal"><span class="pre">cd</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">cddb</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">cdplayer</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">CL</span></tt> and <tt class="xref py py-mod docutils literal"><span class="pre">cl</span></tt>,
<a class="reference internal" href="../library/gl.html#module-DEVICE" title="DEVICE: Constants used with the gl module. (deprecated) (IRIX)"><tt class="xref py py-mod docutils literal"><span class="pre">DEVICE</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">ERRNO</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">FILE</span></tt>,
<a class="reference internal" href="../library/fl.html#module-FL" title="FL: Constants used with the fl module. (deprecated) (IRIX)"><tt class="xref py py-mod docutils literal"><span class="pre">FL</span></tt></a> and <a class="reference internal" href="../library/fl.html#module-fl" title="fl: FORMS library for applications with graphical user interfaces. (deprecated) (IRIX)"><tt class="xref py py-mod docutils literal"><span class="pre">fl</span></tt></a>,
<a class="reference internal" href="../library/fl.html#module-flp" title="flp: Functions for loading stored FORMS designs. (deprecated) (IRIX)"><tt class="xref py py-mod docutils literal"><span class="pre">flp</span></tt></a>,
<a class="reference internal" href="../library/fm.html#module-fm" title="fm: Font Manager interface for SGI workstations. (deprecated) (IRIX)"><tt class="xref py py-mod docutils literal"><span class="pre">fm</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">GET</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">GLWS</span></tt>,
<a class="reference internal" href="../library/gl.html#module-GL" title="GL: Constants used with the gl module. (deprecated) (IRIX)"><tt class="xref py py-mod docutils literal"><span class="pre">GL</span></tt></a> and <a class="reference internal" href="../library/gl.html#module-gl" title="gl: Functions from the Silicon Graphics Graphics Library. (deprecated) (IRIX)"><tt class="xref py py-mod docutils literal"><span class="pre">gl</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">IN</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">IOCTL</span></tt>,
<a class="reference internal" href="../library/jpeg.html#module-jpeg" title="jpeg: Read and write image files in compressed JPEG format. (deprecated) (IRIX)"><tt class="xref py py-mod docutils literal"><span class="pre">jpeg</span></tt></a>,
<tt class="xref py py-mod docutils literal"><span class="pre">panelparser</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">readcd</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">SV</span></tt> and <tt class="xref py py-mod docutils literal"><span class="pre">sv</span></tt>,
<tt class="xref py py-mod docutils literal"><span class="pre">torgb</span></tt>,
<a class="reference internal" href="../library/undoc.html#module-videoreader" title="videoreader: Read QuickTime movies frame by frame for further processing. (deprecated) (Mac)"><tt class="xref py py-mod docutils literal"><span class="pre">videoreader</span></tt></a>, and
<tt class="xref py py-mod docutils literal"><span class="pre">WAIT</span></tt>.</p>
</div>
</div>
<div class="section" id="porting-to-python-2-6">
<h2>Porting to Python 2.6<a class="headerlink" href="#porting-to-python-2-6" title="Permalink to this headline">¶</a></h2>
<p>This section lists previously described changes and other bugfixes
that may require changes to your code:</p>
<ul>
<li><p class="first">Classes that aren&#8217;t supposed to be hashable should
set <tt class="docutils literal"><span class="pre">__hash__</span> <span class="pre">=</span> <span class="pre">None</span></tt> in their definitions to indicate
the fact.</p>
</li>
<li><p class="first">String exceptions have been removed.  Attempting to use them raises a
<a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a>.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a> method of <a class="reference internal" href="../library/collections.html#collections.deque" title="collections.deque"><tt class="xref py py-class docutils literal"><span class="pre">collections.deque</span></tt></a>
now clears any existing contents of the deque
before adding elements from the iterable.  This change makes the
behavior match <tt class="docutils literal"><span class="pre">list.__init__()</span></tt>.</p>
</li>
<li><p class="first"><a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">object.__init__()</span></tt></a> previously accepted arbitrary arguments and
keyword arguments, ignoring them.  In Python 2.6, this is no longer
allowed and will result in a <a class="reference internal" href="../library/exceptions.html#exceptions.TypeError" title="exceptions.TypeError"><tt class="xref py py-exc docutils literal"><span class="pre">TypeError</span></tt></a>.  This will affect
<a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a> methods that end up calling the corresponding
method on <a class="reference internal" href="../library/functions.html#object" title="object"><tt class="xref py py-class docutils literal"><span class="pre">object</span></tt></a> (perhaps through using <a class="reference internal" href="../library/functions.html#super" title="super"><tt class="xref py py-func docutils literal"><span class="pre">super()</span></tt></a>).
See <a class="reference external" href="http://bugs.python.org/issue1683368">issue 1683368</a> for discussion.</p>
</li>
<li><p class="first">The <tt class="xref py py-class docutils literal"><span class="pre">Decimal</span></tt> constructor now accepts leading and trailing
whitespace when passed a string.  Previously it would raise an
<tt class="xref py py-exc docutils literal"><span class="pre">InvalidOperation</span></tt> exception.  On the other hand, the
<tt class="xref py py-meth docutils literal"><span class="pre">create_decimal()</span></tt> method of <tt class="xref py py-class docutils literal"><span class="pre">Context</span></tt> objects now
explicitly disallows extra whitespace, raising a
<tt class="xref py py-exc docutils literal"><span class="pre">ConversionSyntax</span></tt> exception.</p>
</li>
<li><p class="first">Due to an implementation accident, if you passed a file path to
the built-in  <a class="reference internal" href="../library/functions.html#__import__" title="__import__"><tt class="xref py py-func docutils literal"><span class="pre">__import__()</span></tt></a> function, it would actually import
the specified file.  This was never intended to work, however, and
the implementation now explicitly checks for this case and raises
an <a class="reference internal" href="../library/exceptions.html#exceptions.ImportError" title="exceptions.ImportError"><tt class="xref py py-exc docutils literal"><span class="pre">ImportError</span></tt></a>.</p>
</li>
<li><p class="first">C API: the <a class="reference internal" href="../c-api/import.html#PyImport_Import" title="PyImport_Import"><tt class="xref c c-func docutils literal"><span class="pre">PyImport_Import()</span></tt></a> and <a class="reference internal" href="../c-api/import.html#PyImport_ImportModule" title="PyImport_ImportModule"><tt class="xref c c-func docutils literal"><span class="pre">PyImport_ImportModule()</span></tt></a>
functions now default to absolute imports, not relative imports.
This will affect C extensions that import other modules.</p>
</li>
<li><p class="first">C API: extension data types that shouldn&#8217;t be hashable
should define their <tt class="docutils literal"><span class="pre">tp_hash</span></tt> slot to
<a class="reference internal" href="../c-api/object.html#PyObject_HashNotImplemented" title="PyObject_HashNotImplemented"><tt class="xref c c-func docutils literal"><span class="pre">PyObject_HashNotImplemented()</span></tt></a>.</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/socket.html#module-socket" title="socket: Low-level networking interface."><tt class="xref py py-mod docutils literal"><span class="pre">socket</span></tt></a> module exception <a class="reference internal" href="../library/socket.html#socket.error" title="socket.error"><tt class="xref py py-exc docutils literal"><span class="pre">socket.error</span></tt></a> now inherits
from <a class="reference internal" href="../library/exceptions.html#exceptions.IOError" title="exceptions.IOError"><tt class="xref py py-exc docutils literal"><span class="pre">IOError</span></tt></a>.  Previously it wasn&#8217;t a subclass of
<a class="reference internal" href="../library/exceptions.html#exceptions.StandardError" title="exceptions.StandardError"><tt class="xref py py-exc docutils literal"><span class="pre">StandardError</span></tt></a> but now it is, through <a class="reference internal" href="../library/exceptions.html#exceptions.IOError" title="exceptions.IOError"><tt class="xref py py-exc docutils literal"><span class="pre">IOError</span></tt></a>.
(Implemented by Gregory P. Smith; <a class="reference external" href="http://bugs.python.org/issue1706815">issue 1706815</a>.)</p>
</li>
<li><p class="first">The <a class="reference internal" href="../library/xmlrpclib.html#module-xmlrpclib" title="xmlrpclib: XML-RPC client access."><tt class="xref py py-mod docutils literal"><span class="pre">xmlrpclib</span></tt></a> module no longer automatically converts
<a class="reference internal" href="../library/datetime.html#datetime.date" title="datetime.date"><tt class="xref py py-class docutils literal"><span class="pre">datetime.date</span></tt></a> and <a class="reference internal" href="../library/datetime.html#datetime.time" title="datetime.time"><tt class="xref py py-class docutils literal"><span class="pre">datetime.time</span></tt></a> to the
<tt class="xref py py-class docutils literal"><span class="pre">xmlrpclib.DateTime</span></tt> type; the conversion semantics were
not necessarily correct for all applications.  Code using
<a class="reference internal" href="../library/xmlrpclib.html#module-xmlrpclib" title="xmlrpclib: XML-RPC client access."><tt class="xref py py-mod docutils literal"><span class="pre">xmlrpclib</span></tt></a> should convert <tt class="xref py py-class docutils literal"><span class="pre">date</span></tt> and <a class="reference internal" href="../library/time.html#module-time" title="time: Time access and conversions."><tt class="xref py py-class docutils literal"><span class="pre">time</span></tt></a>
instances. (<a class="reference external" href="http://bugs.python.org/issue1330538">issue 1330538</a>)</p>
</li>
<li><p class="first">(3.0-warning mode) The <tt class="xref py py-class docutils literal"><span class="pre">Exception</span></tt> class now warns
when accessed using slicing or index access; having
<tt class="xref py py-class docutils literal"><span class="pre">Exception</span></tt> behave like a tuple is being phased out.</p>
</li>
<li><p class="first">(3.0-warning mode) inequality comparisons between two dictionaries
or two objects that don&#8217;t implement comparison methods are reported
as warnings.  <tt class="docutils literal"><span class="pre">dict1</span> <span class="pre">==</span> <span class="pre">dict2</span></tt> still works, but <tt class="docutils literal"><span class="pre">dict1</span> <span class="pre">&lt;</span> <span class="pre">dict2</span></tt>
is being phased out.</p>
<p>Comparisons between cells, which are an implementation detail of Python&#8217;s
scoping rules, also cause warnings because such comparisons are forbidden
entirely in 3.0.</p>
</li>
</ul>
</div>
<div class="section" id="acknowledgements">
<span id="acks"></span><h2>Acknowledgements<a class="headerlink" href="#acknowledgements" title="Permalink to this headline">¶</a></h2>
<p>The author would like to thank the following people for offering
suggestions, corrections and assistance with various drafts of this
article: Georg Brandl, Steve Brown, Nick Coghlan, Ralph Corderoy,
Jim Jewett, Kent Johnson, Chris Lambacher,  Martin Michlmayr,
Antoine Pitrou, Brian Warner.</p>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">What&#8217;s New in Python 2.6</a><ul>
<li><a class="reference internal" href="#python-3-0">Python 3.0</a></li>
<li><a class="reference internal" href="#changes-to-the-development-process">Changes to the Development Process</a><ul>
<li><a class="reference internal" href="#new-issue-tracker-roundup">New Issue Tracker: Roundup</a></li>
<li><a class="reference internal" href="#new-documentation-format-restructuredtext-using-sphinx">New Documentation Format: reStructuredText Using Sphinx</a></li>
</ul>
</li>
<li><a class="reference internal" href="#pep-343-the-with-statement">PEP 343: The &#8216;with&#8217; statement</a><ul>
<li><a class="reference internal" href="#writing-context-managers">Writing Context Managers</a></li>
<li><a class="reference internal" href="#the-contextlib-module">The contextlib module</a></li>
</ul>
</li>
<li><a class="reference internal" href="#pep-366-explicit-relative-imports-from-a-main-module">PEP 366: Explicit Relative Imports From a Main Module</a></li>
<li><a class="reference internal" href="#pep-370-per-user-site-packages-directory">PEP 370: Per-user <tt class="docutils literal"><span class="pre">site-packages</span></tt> Directory</a></li>
<li><a class="reference internal" href="#pep-371-the-multiprocessing-package">PEP 371: The <tt class="docutils literal"><span class="pre">multiprocessing</span></tt> Package</a></li>
<li><a class="reference internal" href="#pep-3101-advanced-string-formatting">PEP 3101: Advanced String Formatting</a></li>
<li><a class="reference internal" href="#pep-3105-print-as-a-function">PEP 3105: <tt class="docutils literal"><span class="pre">print</span></tt> As a Function</a></li>
<li><a class="reference internal" href="#pep-3110-exception-handling-changes">PEP 3110: Exception-Handling Changes</a></li>
<li><a class="reference internal" href="#pep-3112-byte-literals">PEP 3112: Byte Literals</a></li>
<li><a class="reference internal" href="#pep-3116-new-i-o-library">PEP 3116: New I/O Library</a></li>
<li><a class="reference internal" href="#pep-3118-revised-buffer-protocol">PEP 3118: Revised Buffer Protocol</a></li>
<li><a class="reference internal" href="#pep-3119-abstract-base-classes">PEP 3119: Abstract Base Classes</a></li>
<li><a class="reference internal" href="#pep-3127-integer-literal-support-and-syntax">PEP 3127: Integer Literal Support and Syntax</a></li>
<li><a class="reference internal" href="#pep-3129-class-decorators">PEP 3129: Class Decorators</a></li>
<li><a class="reference internal" href="#pep-3141-a-type-hierarchy-for-numbers">PEP 3141: A Type Hierarchy for Numbers</a><ul>
<li><a class="reference internal" href="#the-fractions-module">The <tt class="docutils literal"><span class="pre">fractions</span></tt> Module</a></li>
</ul>
</li>
<li><a class="reference internal" href="#other-language-changes">Other Language Changes</a><ul>
<li><a class="reference internal" href="#optimizations">Optimizations</a></li>
<li><a class="reference internal" href="#interpreter-changes">Interpreter Changes</a></li>
</ul>
</li>
<li><a class="reference internal" href="#new-and-improved-modules">New and Improved Modules</a><ul>
<li><a class="reference internal" href="#the-ast-module">The <tt class="docutils literal"><span class="pre">ast</span></tt> module</a></li>
<li><a class="reference internal" href="#the-future-builtins-module">The <tt class="docutils literal"><span class="pre">future_builtins</span></tt> module</a></li>
<li><a class="reference internal" href="#the-json-module-javascript-object-notation">The <tt class="docutils literal"><span class="pre">json</span></tt> module: JavaScript Object Notation</a></li>
<li><a class="reference internal" href="#the-plistlib-module-a-property-list-parser">The <tt class="docutils literal"><span class="pre">plistlib</span></tt> module: A Property-List Parser</a></li>
<li><a class="reference internal" href="#ctypes-enhancements">ctypes Enhancements</a></li>
<li><a class="reference internal" href="#improved-ssl-support">Improved SSL Support</a></li>
</ul>
</li>
<li><a class="reference internal" href="#deprecations-and-removals">Deprecations and Removals</a></li>
<li><a class="reference internal" href="#build-and-c-api-changes">Build and C API Changes</a><ul>
<li><a class="reference internal" href="#port-specific-changes-windows">Port-Specific Changes: Windows</a></li>
<li><a class="reference internal" href="#port-specific-changes-mac-os-x">Port-Specific Changes: Mac OS X</a></li>
<li><a class="reference internal" href="#port-specific-changes-irix">Port-Specific Changes: IRIX</a></li>
</ul>
</li>
<li><a class="reference internal" href="#porting-to-python-2-6">Porting to Python 2.6</a></li>
<li><a class="reference internal" href="#acknowledgements">Acknowledgements</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="2.7.html"
                        title="previous chapter">What&#8217;s New in Python 2.7</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="2.5.html"
                        title="next chapter">What&#8217;s New in Python 2.5</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/whatsnew/2.6.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" size="18" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="2.5.html" title="What’s New in Python 2.5"
             >next</a> |</li>
        <li class="right" >
          <a href="2.7.html" title="What’s New in Python 2.7"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="http://www.python.org/">Python</a> &raquo;</li>
        <li>
          <a href="../index.html">Python v2.7.8 documentation</a> &raquo;
        </li>

          <li><a href="index.html" >What&#8217;s New in Python</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2014, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="http://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Jun 29, 2014.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7.
    </div>

  </body>
</html>